ScriptManager.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Automation/ScriptManager.h>
  9. #include <Automation/ScriptableImGui.h>
  10. #include <SampleComponentManagerBus.h>
  11. #include <imgui/imgui_internal.h>
  12. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  13. #include <Atom/RPI.Public/RPISystemInterface.h>
  14. #include <Atom/Component/DebugCamera/CameraComponent.h>
  15. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  16. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  17. #include <Atom/Feature/ImGui/SystemBus.h>
  18. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  19. #include <Atom/RHI/Factory.h>
  20. #include <AzCore/Component/Entity.h>
  21. #include <AzCore/Script/ScriptContext.h>
  22. #include <AzCore/Script/ScriptSystemBus.h>
  23. #include <AzCore/Script/ScriptAsset.h>
  24. #include <AzCore/Math/MathReflection.h>
  25. #include <AzCore/Console/IConsole.h>
  26. #include <AzFramework/API/ApplicationAPI.h>
  27. #include <AzFramework/Components/ConsoleBus.h>
  28. #include <AzFramework/IO/LocalFileIO.h>
  29. #include <AzFramework/Windowing/WindowBus.h>
  30. #include <AtomSampleViewerRequestBus.h>
  31. #include <Utils/Utils.h>
  32. namespace AtomSampleViewer
  33. {
  34. ScriptManager* ScriptManager::s_instance = nullptr;
  35. ScriptManager::ScriptManager()
  36. : m_scriptBrowser("@user@/lua_script_browser.xml")
  37. {
  38. }
  39. void ScriptManager::Activate()
  40. {
  41. AZ_Assert(s_instance == nullptr, "ScriptManager is already activated");
  42. s_instance = this;
  43. ScriptableImGui::Create();
  44. m_scriptContext = AZStd::make_unique<AZ::ScriptContext>();
  45. m_sriptBehaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
  46. ReflectScriptContext(m_sriptBehaviorContext.get());
  47. m_scriptContext->BindTo(m_sriptBehaviorContext.get());
  48. m_scriptBrowser.SetFilter([](const AZ::Data::AssetInfo& assetInfo)
  49. {
  50. return AzFramework::StringFunc::EndsWith(assetInfo.m_relativePath, ".bv.luac");
  51. });
  52. m_scriptBrowser.Activate();
  53. ScriptRepeaterRequestBus::Handler::BusConnect();
  54. ScriptRunnerRequestBus::Handler::BusConnect();
  55. m_imageComparisonOptions.Activate();
  56. }
  57. void ScriptManager::Deactivate()
  58. {
  59. s_instance = nullptr;
  60. m_scriptContext = nullptr;
  61. m_sriptBehaviorContext = nullptr;
  62. m_scriptBrowser.Deactivate();
  63. ScriptableImGui::Destory();
  64. m_imageComparisonOptions.Deactivate();
  65. ScriptRunnerRequestBus::Handler::BusDisconnect();
  66. ScriptRepeaterRequestBus::Handler::BusDisconnect();
  67. AZ::Debug::CameraControllerNotificationBus::Handler::BusDisconnect();
  68. }
  69. void ScriptManager::SetCameraEntity(AZ::Entity* cameraEntity)
  70. {
  71. AZ::Debug::CameraControllerNotificationBus::Handler::BusDisconnect();
  72. m_cameraEntity = cameraEntity;
  73. AZ::Debug::CameraControllerNotificationBus::Handler::BusConnect(m_cameraEntity->GetId());
  74. }
  75. void ScriptManager::PauseScript()
  76. {
  77. PauseScriptWithTimeout(DefaultPauseTimeout);
  78. }
  79. void ScriptManager::PauseScriptWithTimeout(float timeout)
  80. {
  81. m_scriptPaused = true;
  82. m_scriptPauseTimeout = AZ::GetMax(timeout, m_scriptPauseTimeout);
  83. }
  84. void ScriptManager::ResumeScript()
  85. {
  86. AZ_Warning("Automation", m_scriptPaused, "Script is not paused");
  87. m_scriptPaused = false;
  88. }
  89. void ScriptManager::ReportScriptError([[maybe_unused]] const AZStd::string& message)
  90. {
  91. AZ_Error("Automation", false, "Script: %s", message.c_str());
  92. }
  93. void ScriptManager::ReportScriptWarning([[maybe_unused]] const AZStd::string& message)
  94. {
  95. AZ_Warning("Automation", false, "Script: %s", message.c_str());
  96. }
  97. void ScriptManager::ReportScriptableAction([[maybe_unused]] AZStd::string_view scriptCommand)
  98. {
  99. AZ_TracePrintf("Automation", "Scriptable Action: %.*s\n", AZ_STRING_ARG(scriptCommand));
  100. }
  101. void ScriptManager::ImageComparisonOptions::Activate()
  102. {
  103. m_configAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::AnyAsset>("config/ImageComparisonConfig.azasset", AZ::RPI::AssetUtils::TraceLevel::Assert);
  104. if (m_configAsset)
  105. {
  106. AZ::Data::AssetBus::Handler::BusConnect(m_configAsset.GetId());
  107. OnAssetReloaded(m_configAsset);
  108. }
  109. }
  110. void ScriptManager::ImageComparisonOptions::Deactivate()
  111. {
  112. m_configAsset.Release();
  113. ResetImGuiSettings();
  114. AZ::Data::AssetBus::Handler::BusDisconnect();
  115. }
  116. ImageComparisonToleranceLevel* ScriptManager::ImageComparisonOptions::FindToleranceLevel(const AZStd::string& name, bool allowLevelAdjustment)
  117. {
  118. size_t foundIndex = m_config.m_toleranceLevels.size();
  119. for (size_t i = 0; i < m_config.m_toleranceLevels.size(); ++i)
  120. {
  121. if (m_config.m_toleranceLevels[i].m_name == name)
  122. {
  123. foundIndex = i;
  124. break;
  125. }
  126. }
  127. if (foundIndex == m_config.m_toleranceLevels.size())
  128. {
  129. return nullptr;
  130. }
  131. if (allowLevelAdjustment)
  132. {
  133. int adjustedIndex = aznumeric_cast<int>(foundIndex);
  134. adjustedIndex += m_toleranceAdjustment;
  135. adjustedIndex = AZ::GetClamp(adjustedIndex, 0, aznumeric_cast<int>(m_config.m_toleranceLevels.size()) - 1);
  136. foundIndex = aznumeric_cast<size_t>(adjustedIndex);
  137. }
  138. return &m_config.m_toleranceLevels[foundIndex];
  139. }
  140. const AZStd::vector<ImageComparisonToleranceLevel>& ScriptManager::ImageComparisonOptions::GetAvailableToleranceLevels() const
  141. {
  142. return m_config.m_toleranceLevels;
  143. }
  144. void ScriptManager::ImageComparisonOptions::SelectToleranceLevel(const AZStd::string& name, bool allowLevelAdjustment)
  145. {
  146. if (m_selectedOverrideSetting == OverrideSetting_ScriptControlled)
  147. {
  148. ImageComparisonToleranceLevel* level = FindToleranceLevel(name, allowLevelAdjustment);
  149. if (level)
  150. {
  151. m_currentToleranceLevel = level;
  152. }
  153. else
  154. {
  155. ReportScriptError(AZStd::string::format("ImageComparisonToleranceLevel '%s' not found.", name.c_str()));
  156. }
  157. }
  158. }
  159. void ScriptManager::ImageComparisonOptions::SelectToleranceLevel(ImageComparisonToleranceLevel* level)
  160. {
  161. if (nullptr == level)
  162. {
  163. m_currentToleranceLevel = level;
  164. return;
  165. }
  166. else
  167. {
  168. SelectToleranceLevel(level->m_name);
  169. AZ_Assert(GetCurrentToleranceLevel() == level, "Wrong ImageComparisonToleranceLevel pointer used");
  170. }
  171. }
  172. ImageComparisonToleranceLevel* ScriptManager::ImageComparisonOptions::GetCurrentToleranceLevel()
  173. {
  174. return m_currentToleranceLevel;
  175. }
  176. bool ScriptManager::ImageComparisonOptions::IsScriptControlled() const
  177. {
  178. return m_selectedOverrideSetting == OverrideSetting_ScriptControlled;
  179. }
  180. bool ScriptManager::ImageComparisonOptions::IsLevelAdjusted() const
  181. {
  182. return m_toleranceAdjustment != 0;
  183. }
  184. void ScriptManager::ImageComparisonOptions::DrawImGuiSettings()
  185. {
  186. ImGui::Text("Tolerance");
  187. ImGui::Indent();
  188. if (ImGui::Combo("Level",
  189. &m_selectedOverrideSetting,
  190. m_overrideSettings.data(),
  191. aznumeric_cast<int>(m_overrideSettings.size())))
  192. {
  193. if (m_selectedOverrideSetting == OverrideSetting_ScriptControlled)
  194. {
  195. m_currentToleranceLevel = nullptr;
  196. }
  197. else
  198. {
  199. m_currentToleranceLevel = &m_config.m_toleranceLevels[m_selectedOverrideSetting - 1];
  200. }
  201. }
  202. if (IsScriptControlled())
  203. {
  204. ImGui::InputInt("Level Adjustment", &m_toleranceAdjustment);
  205. }
  206. ImGui::Unindent();
  207. }
  208. void ScriptManager::ImageComparisonOptions::ResetImGuiSettings()
  209. {
  210. m_currentToleranceLevel = nullptr;
  211. m_selectedOverrideSetting = OverrideSetting_ScriptControlled;
  212. m_toleranceAdjustment = 0;
  213. }
  214. void ScriptManager::ImageComparisonOptions::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  215. {
  216. m_configAsset = asset;
  217. m_config = *m_configAsset->GetDataAs<ImageComparisonConfig>();
  218. m_overrideSettings.clear();
  219. m_overrideSettings.push_back("[Script-controlled]");
  220. for (size_t i = 0; i < m_config.m_toleranceLevels.size(); ++i)
  221. {
  222. AZ_Assert(i == 0 || m_config.m_toleranceLevels[i].m_threshold > m_config.m_toleranceLevels[i - 1].m_threshold, "Threshold values are not sequential");
  223. m_overrideSettings.push_back(m_config.m_toleranceLevels[i].m_name.c_str());
  224. }
  225. }
  226. void ScriptManager::TickImGui()
  227. {
  228. if (m_showScriptRunnerDialog)
  229. {
  230. ShowScriptRunnerDialog();
  231. }
  232. m_scriptReporter.TickImGui();
  233. if (m_showPrecommitWizard)
  234. {
  235. ShowPrecommitWizard();
  236. }
  237. if (m_testSuiteRunConfig.m_automatedRunEnabled)
  238. {
  239. if (m_testSuiteRunConfig.m_isStarted == false)
  240. {
  241. m_testSuiteRunConfig.m_isStarted = true;
  242. PrepareAndExecuteScript(m_testSuiteRunConfig.m_testSuitePath);
  243. }
  244. }
  245. }
  246. void ScriptManager::TickScript(float deltaTime)
  247. {
  248. // All actions must be consumed each frame. Otherwise, this indicates that a script is
  249. // scheduling ScriptableImGui actions for fields that don't exist.
  250. ScriptableImGui::CheckAllActionsConsumed();
  251. ScriptableImGui::ClearActions();
  252. // We delayed PopScript() until after the above CheckAllActionsConsumed(), so that any errors
  253. // reported by that function will be associated with the proper script.
  254. if (m_shouldPopScript)
  255. {
  256. m_scriptReporter.PopScript();
  257. m_shouldPopScript = false;
  258. }
  259. while (!m_scriptOperations.empty())
  260. {
  261. if (m_shouldPopScript)
  262. {
  263. // If we just finished executing a script, the remaining m_scriptOperations are for some other script.
  264. // We need to proceed to the next frame and allow that PopScript() to happen, otherwise any errors related
  265. // to subsequent operations would be reported against the prior script.
  266. break;
  267. }
  268. if (m_scriptPaused)
  269. {
  270. m_scriptPauseTimeout -= deltaTime;
  271. if (m_scriptPauseTimeout < 0)
  272. {
  273. AZ_Error("Automation", false, "Script pause timed out. Continuing...");
  274. m_scriptPaused = false;
  275. }
  276. else
  277. {
  278. break;
  279. }
  280. }
  281. if (m_waitForAssetTracker)
  282. {
  283. m_assetTrackingTimeout -= deltaTime;
  284. if (m_assetTrackingTimeout < 0)
  285. {
  286. auto incomplateAssetList = m_assetStatusTracker.GetIncompleteAssetList();
  287. AZStd::string incompleteAssetListString;
  288. AzFramework::StringFunc::Join(incompleteAssetListString, incomplateAssetList.begin(), incomplateAssetList.end(), "\n ");
  289. AZ_Error("Automation", false, "Script asset tracking timed out waiting for:\n %s \n Continuing...", incompleteAssetListString.c_str());
  290. m_waitForAssetTracker = false;
  291. }
  292. else if (m_assetStatusTracker.DidExpectedAssetsFinish())
  293. {
  294. AZ_Printf("Automation", "Asset Tracker finished with %f seconds remaining.", m_assetTrackingTimeout);
  295. m_waitForAssetTracker = false;
  296. }
  297. else
  298. {
  299. break;
  300. }
  301. }
  302. if (m_scriptIdleFrames > 0)
  303. {
  304. m_scriptIdleFrames--;
  305. break;
  306. }
  307. if (m_scriptIdleSeconds > 0)
  308. {
  309. m_scriptIdleSeconds -= deltaTime;
  310. break;
  311. }
  312. // Execute the next operation
  313. m_scriptOperations.front()();
  314. m_scriptOperations.pop();
  315. if (m_scriptOperations.empty())
  316. {
  317. m_doFinalScriptCleanup = true;
  318. }
  319. }
  320. if (m_shouldPopScript)
  321. {
  322. // We need to proceed for one more frame to do the last PopScript() before final cleanup
  323. return;
  324. }
  325. if (m_doFinalScriptCleanup)
  326. {
  327. bool frameCapturePending = false;
  328. SampleComponentManagerRequestBus::BroadcastResult(frameCapturePending, &SampleComponentManagerRequests::IsFrameCapturePending);
  329. if (!frameCapturePending && !m_isCapturePending)
  330. {
  331. AZ_Assert(m_scriptPaused == false, "Script manager is in an unexpected state.");
  332. AZ_Assert(m_scriptIdleFrames == 0, "Script manager is in an unexpected state.");
  333. AZ_Assert(m_scriptIdleSeconds <= 0.0f, "Script manager is in an unexpected state.");
  334. AZ_Assert(m_waitForAssetTracker == false, "Script manager is in an unexpected state.");
  335. AZ_Assert(!m_scriptReporter.HasActiveScript(), "Script manager is in an unexpected state.");
  336. AZ_Assert(m_executingScripts.size() == 0, "Script manager is in an unexpected state");
  337. m_assetStatusTracker.StopTracking();
  338. if (m_frameTimeIsLocked)
  339. {
  340. AZ::Interface<AZ::IConsole>::Get()->PerformCommand("t_simulationTickDeltaOverride 0");
  341. m_frameTimeIsLocked = false;
  342. }
  343. if (m_shouldRestoreViewportSize)
  344. {
  345. Utils::ResizeClientArea(m_savedViewportWidth, m_savedViewportHeight);
  346. m_shouldRestoreViewportSize = false;
  347. }
  348. // In case scripts were aborted while ImGui was temporarily hidden, show it again.
  349. SetShowImGui(true);
  350. m_scriptReporter.SortScriptReports();
  351. m_scriptReporter.OpenReportDialog();
  352. m_shouldPopScript = false;
  353. m_doFinalScriptCleanup = false;
  354. if (m_testSuiteRunConfig.m_automatedRunEnabled && m_testSuiteRunConfig.m_closeOnTestScriptFinish)
  355. {
  356. m_testSuiteRunConfig.m_automatedRunEnabled = false;
  357. if (m_scriptReporter.HasErrorsAssertsInReport())
  358. {
  359. AtomSampleViewerRequestsBus::Broadcast(&AtomSampleViewerRequestsBus::Events::SetExitCode, 1);
  360. // Useful console logging for Hydra tests
  361. int failedTests = 0;
  362. for (const ScriptReporter::ScriptReport& testReport : m_scriptReporter.GetScriptReport())
  363. {
  364. if (testReport.m_assertCount > 0 || testReport.m_generalErrorCount > 0 || testReport.m_screenshotErrorCount > 0)
  365. {
  366. ++failedTests;
  367. AZ_Printf("AtomSampleViewer", "Test failure %s: asserts %u, general errors %u, screenshot failures %u\n", testReport.m_scriptAssetPath.c_str(),
  368. testReport.m_assertCount, testReport.m_generalErrorCount, testReport.m_screenshotErrorCount);
  369. }
  370. }
  371. AZ_Printf("AtomSampleViewer", "%d tests failed\n", failedTests);
  372. }
  373. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
  374. }
  375. }
  376. }
  377. }
  378. void ScriptManager::OpenScriptRunnerDialog()
  379. {
  380. m_showScriptRunnerDialog = true;
  381. }
  382. void ScriptManager::OpenPrecommitWizard()
  383. {
  384. m_showPrecommitWizard = true;
  385. }
  386. void ScriptManager::RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed)
  387. {
  388. m_testSuiteRunConfig.m_automatedRunEnabled = true;
  389. m_testSuiteRunConfig.m_testSuitePath = suiteFilePath;
  390. m_testSuiteRunConfig.m_closeOnTestScriptFinish = exitOnTestEnd;
  391. m_testSuiteRunConfig.m_randomSeed = randomSeed;
  392. }
  393. void ScriptManager::AbortScripts(const AZStd::string& reason)
  394. {
  395. m_scriptReporter.SetInvalidationMessage(reason);
  396. m_scriptOperations = {};
  397. m_executingScripts.clear();
  398. m_scriptPaused = false;
  399. m_scriptIdleFrames = 0;
  400. m_scriptIdleSeconds = 0.0f;
  401. m_waitForAssetTracker = false;
  402. while (m_scriptReporter.HasActiveScript())
  403. {
  404. m_scriptReporter.PopScript();
  405. }
  406. m_doFinalScriptCleanup = true;
  407. }
  408. void ScriptManager::ShowScriptRunnerDialog()
  409. {
  410. if (ImGui::Begin("Script Runner", &m_showScriptRunnerDialog))
  411. {
  412. auto drawAbortButton = [this](const char* uniqueId)
  413. {
  414. ImGui::PushID(uniqueId);
  415. if (ImGui::Button("Abort"))
  416. {
  417. AbortScripts("Script(s) manually aborted.");
  418. }
  419. ImGui::PopID();
  420. };
  421. // The main buttons are at the bottom, but show the Abort button at the top too, in case the window size is small.
  422. if (!m_scriptOperations.empty())
  423. {
  424. drawAbortButton("Button1");
  425. }
  426. ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
  427. assetBrowserSettings.m_labels.m_root = "Lua Scripts";
  428. m_scriptBrowser.Tick(assetBrowserSettings);
  429. AZStd::string selectedFileName = "<none>";
  430. AzFramework::StringFunc::Path::GetFullFileName(m_scriptBrowser.GetSelectedAssetPath().c_str(), selectedFileName);
  431. ImGui::LabelText("##SelectedScript", "Selected: %s", selectedFileName.c_str());
  432. ImGui::Separator();
  433. ImGui::Text("Settings");
  434. ImGui::Indent();
  435. ImGui::InputInt("Random Seed for Test Order Execution", &m_testSuiteRunConfig.m_randomSeed);
  436. m_imageComparisonOptions.DrawImGuiSettings();
  437. if (ImGui::Button("Reset"))
  438. {
  439. m_imageComparisonOptions.ResetImGuiSettings();
  440. }
  441. ImGui::Unindent();
  442. ImGui::Separator();
  443. if (ImGui::Button("Run"))
  444. {
  445. auto scriptAsset = m_scriptBrowser.GetSelectedAsset<AZ::ScriptAsset>();
  446. if (scriptAsset.GetId().IsValid())
  447. {
  448. PrepareAndExecuteScript(m_scriptBrowser.GetSelectedAssetPath());
  449. }
  450. }
  451. if (ImGui::Button("View Latest Results"))
  452. {
  453. m_scriptReporter.OpenReportDialog();
  454. }
  455. if (m_scriptOperations.size() > 0)
  456. {
  457. ImGui::LabelText("##RunningScript", "Running %zu operations...", m_scriptOperations.size());
  458. drawAbortButton("Button2");
  459. }
  460. }
  461. m_messageBox.TickPopup();
  462. ImGui::End();
  463. }
  464. void ScriptManager::ShowPrecommitWizard()
  465. {
  466. if (ImGui::Begin("Pre-commit Wizard", &m_showPrecommitWizard))
  467. {
  468. ShowBackToIntroWarning();
  469. if (ImGui::Button("Start from the Beginning"))
  470. {
  471. ImGui::OpenPopup("Return to Intro Window");
  472. }
  473. switch (m_wizardSettings.m_stage)
  474. {
  475. case PrecommitWizardSettings::Stage::Intro:
  476. ShowPrecommitWizardIntro();
  477. break;
  478. case PrecommitWizardSettings::Stage::RunFullsuiteTest:
  479. ShowPrecommitWizardRunFullsuiteTest();
  480. break;
  481. case PrecommitWizardSettings::Stage::ReportFullsuiteSummary:
  482. ShowPrecommitWizardReportFullsuiteTest();
  483. break;
  484. case PrecommitWizardSettings::Stage::ManualInspection:
  485. // Go through each pass in the full suite test and require users to manually pick from a list
  486. // to describe the difference if it exists.
  487. ShowPrecomitWizardManualInspection();
  488. break;
  489. case PrecommitWizardSettings::Stage::ReportFinalSummary:
  490. // Generate a summary that can be easily copied into the clipboard
  491. ShowPrecommitWizardReportFinalSummary();
  492. break;
  493. }
  494. }
  495. ImGui::End();
  496. }
  497. void ScriptManager::ShowBackToIntroWarning()
  498. {
  499. if (ImGui::BeginPopupModal("Return to Intro Window", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
  500. {
  501. ImGui::Text("Warning: You are returning to the intro window. Current full test suite results will be lost.\n\nAre you sure?");
  502. if (ImGui::Button("Yes"))
  503. {
  504. m_wizardSettings = PrecommitWizardSettings();
  505. ImGui::CloseCurrentPopup();
  506. }
  507. ImGui::SameLine();
  508. if (ImGui::Button("No"))
  509. {
  510. ImGui::CloseCurrentPopup();
  511. }
  512. ImGui::EndPopup();
  513. }
  514. }
  515. void ScriptManager::ShowPrecommitWizardIntro()
  516. {
  517. ImGui::Separator();
  518. ImGui::TextWrapped("Here's what will take place...\n"
  519. "1) The standard '_fulltestsuite_' script will be run.\n"
  520. "2) You'll see a summary of the test results. If any tests failed the automatic checks, "
  521. "you are strongly encouraged to address those issues first.\n"
  522. "3) You will be guided through a series of visual screenshot validations. For each one, "
  523. "you'll need to inspect an image comparison and indicate whether you see visual differences.\n"
  524. "4) The final report will be generated. Copy and paste this into your PR description.");
  525. ImGui::Separator();
  526. if (ImGui::Button("Run Full Test Suite"))
  527. {
  528. PrepareAndExecuteScript(FullSuiteScriptFilepath);
  529. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::RunFullsuiteTest;
  530. }
  531. }
  532. void ScriptManager::ShowPrecommitWizardRunFullsuiteTest()
  533. {
  534. ImGui::Text("Running Full Test Suite. Please Wait...");
  535. if (m_scriptOperations.empty() && !m_doFinalScriptCleanup)
  536. {
  537. m_scriptReporter.HideReportDialog();
  538. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFullsuiteSummary;
  539. }
  540. }
  541. void ScriptManager::ShowPrecommitWizardReportFullsuiteTest()
  542. {
  543. m_scriptReporter.DisplayScriptResultsSummary();
  544. if (ImGui::Button("See Details..."))
  545. {
  546. m_scriptReporter.OpenReportDialog();
  547. }
  548. ImGui::Separator();
  549. if (ImGui::Button("Back"))
  550. {
  551. ImGui::OpenPopup("Return to Intro Window");
  552. }
  553. ImGui::SameLine();
  554. if (ImGui::Button("Continue"))
  555. {
  556. m_wizardSettings.ProcessScriptReports(m_scriptReporter.GetScriptReport());
  557. if (!m_wizardSettings.m_reportsOrderedByThresholdToInspect.empty())
  558. {
  559. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ManualInspection;
  560. }
  561. else
  562. {
  563. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFinalSummary;
  564. }
  565. }
  566. }
  567. void ScriptManager::ShowPrecomitWizardManualInspection()
  568. {
  569. const AZStd::vector<ScriptReporter::ScriptReport>& scriptReports = m_scriptReporter.GetScriptReport();
  570. // Get the script report and screenshot test corresponding to the current index
  571. const ScriptReporter::ReportIndex currentIndex = m_wizardSettings.m_reportIterator->second;
  572. const ScriptReporter::ScriptReport& scriptReport = scriptReports[currentIndex.first];
  573. const ScriptReporter::ScreenshotTestInfo& screenshotTest = scriptReport.m_screenshotTests[currentIndex.second];
  574. ImGui::Text("Script Name: %s", scriptReport.m_scriptAssetPath.c_str());
  575. ImGui::Text("Screenshot Name: %s", screenshotTest.m_officialBaselineScreenshotFilePath.c_str());
  576. ImGui::Separator();
  577. ImGui::Text("Diff Score: %f", screenshotTest.m_officialComparisonResult.m_finalDiffScore);
  578. // TODO: Render screenshots in ImGui.
  579. ImGui::Separator();
  580. // Check if the current index's screenshot has been manually validated and let the user know
  581. auto it = m_wizardSettings.m_reportIndexDifferenceLevelMap.find(currentIndex);
  582. if (it != m_wizardSettings.m_reportIndexDifferenceLevelMap.end())
  583. {
  584. m_wizardSettings.m_inspectionSelection = it->second;
  585. ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 255, 0, 255));
  586. ImGui::Text("This inspection has been added to the report summary");
  587. ImGui::PopStyleColor();
  588. }
  589. else
  590. {
  591. ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 0, 0, 255));
  592. ImGui::Text("This inspection has not been added to the report summary");
  593. ImGui::PopStyleColor();
  594. }
  595. ImGui::TextWrapped(
  596. "Use the 'Export Png' button to inspect the screenshot. Choose from the options below that describe the difference level "
  597. "then click 'Next' to add it in the report summary. Once there are no more noticeable differences as the threshold decreases, "
  598. "click 'Finish' to generate the report summary. Note that the remaining screenshots that were not manually inspected will not "
  599. "appear in the report summary.");
  600. for (int i = 0; i < 4; ++i)
  601. {
  602. ImGui::RadioButton(PrecommitWizardSettings::ManualInspectionOptions[i], &m_wizardSettings.m_inspectionSelection, i);
  603. }
  604. if (ImGui::Button("View Diff"))
  605. {
  606. if (!Utils::RunDiffTool(screenshotTest.m_officialBaselineScreenshotFilePath, screenshotTest.m_screenshotFilePath))
  607. {
  608. ImGui::OpenPopup("Cannot open diff tool");
  609. }
  610. }
  611. if (ImGui::BeginPopupModal("Cannot open diff tool", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
  612. {
  613. ImGui::Text("Image diff is not supported on this platform, or the required diff tool is not installed.");
  614. if (ImGui::Button("Ok"))
  615. {
  616. ImGui::CloseCurrentPopup();
  617. }
  618. ImGui::EndPopup();
  619. }
  620. ImGui::SameLine();
  621. if (ImGui::Button("Export Png"))
  622. {
  623. m_wizardSettings.m_exportedPngPath = m_scriptReporter.ExportImageDiff(scriptReport, screenshotTest);
  624. }
  625. if (!m_wizardSettings.m_exportedPngPath.empty())
  626. {
  627. ImGui::SameLine();
  628. bool copyPath = ImGui::Button("Copy Path");
  629. ImGui::Text("Diff Png was exported to:");
  630. ImGui::SameLine();
  631. if (copyPath)
  632. {
  633. ImGui::LogToClipboard();
  634. }
  635. ImGui::Text("%s", m_wizardSettings.m_exportedPngPath.c_str());
  636. if (copyPath)
  637. {
  638. ImGui::LogFinish();
  639. }
  640. }
  641. ImGui::Separator();
  642. if (ImGui::Button("Back"))
  643. {
  644. if (m_wizardSettings.m_reportIterator == m_wizardSettings.m_reportsOrderedByThresholdToInspect.begin())
  645. {
  646. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFullsuiteSummary;
  647. }
  648. else
  649. {
  650. --m_wizardSettings.m_reportIterator;
  651. }
  652. }
  653. ImGui::SameLine();
  654. const bool isNextButtonDisabled =
  655. m_wizardSettings.m_inspectionSelection == PrecommitWizardSettings::DefaultInspectionSelection ? true : false;
  656. if (isNextButtonDisabled)
  657. {
  658. ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
  659. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
  660. }
  661. if (ImGui::Button("Next"))
  662. {
  663. m_wizardSettings.m_reportIndexDifferenceLevelMap[currentIndex] =
  664. PrecommitWizardSettings::ImageDifferenceLevel::Levels(m_wizardSettings.m_inspectionSelection);
  665. m_wizardSettings.m_inspectionSelection = PrecommitWizardSettings::DefaultInspectionSelection;
  666. m_wizardSettings.m_exportedPngPath.clear();
  667. ++m_wizardSettings.m_reportIterator;
  668. if (m_wizardSettings.m_reportIterator == m_wizardSettings.m_reportsOrderedByThresholdToInspect.end())
  669. {
  670. // Decrement the iterator to make sure it's pointing to the last element in case the user goes
  671. // back to the manual inspection page.
  672. --m_wizardSettings.m_reportIterator;
  673. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFinalSummary;
  674. }
  675. }
  676. if (isNextButtonDisabled)
  677. {
  678. ImGui::PopItemFlag();
  679. ImGui::PopStyleVar();
  680. }
  681. ImGui::SameLine();
  682. if (ImGui::Button("Finish Manual Inspection"))
  683. {
  684. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFinalSummary;
  685. }
  686. ImGui::SameLine();
  687. ImGui::Text(
  688. "%zu/%zu", aznumeric_cast<size_t>(AZStd::distance(m_wizardSettings.m_reportsOrderedByThresholdToInspect.begin(), m_wizardSettings.m_reportIterator) + 1),
  689. m_wizardSettings.m_reportsOrderedByThresholdToInspect.size());
  690. }
  691. void ScriptManager::ShowPrecommitWizardReportFinalSummary()
  692. {
  693. ImGui::Text("Tests are complete! here is the final report.");
  694. bool copyToClipboard = ImGui::Button("Copy To Clipboard");
  695. const ScriptReporter::ScriptResultsSummary resultsSummary = m_scriptReporter.GetScriptResultSummary();
  696. const AZStd::vector<ScriptReporter::ScriptReport>& scriptReports = m_scriptReporter.GetScriptReport();
  697. AZStd::vector<AZStd::vector<ScriptReporter::ReportIndex>> imageDifferenceSummary(
  698. PrecommitWizardSettings::ImageDifferenceLevel::NumDifferenceLevels);
  699. int totalValidatedScreenshots = 0;
  700. for (const auto& [reportIndex, differenceLevel] : m_wizardSettings.m_reportIndexDifferenceLevelMap)
  701. {
  702. imageDifferenceSummary[differenceLevel].push_back(reportIndex);
  703. }
  704. for (const auto& differenceLevelReports : imageDifferenceSummary)
  705. {
  706. totalValidatedScreenshots += aznumeric_cast<int>(differenceLevelReports.size());
  707. }
  708. ImGui::Separator();
  709. if (copyToClipboard)
  710. {
  711. ImGui::LogToClipboard();
  712. }
  713. ImGui::Text("RHI API: %s", AZ::RHI::Factory::Get().GetName().GetCStr());
  714. ImGui::Text("Test Script Count: %zu", scriptReports.size());
  715. ImGui::Text("Screenshot Count: %d", resultsSummary.m_totalScreenshotsCount);
  716. ImGui::Text(
  717. "Manually Validated Screenshots: %d/%zu", totalValidatedScreenshots,
  718. m_wizardSettings.m_reportsOrderedByThresholdToInspect.size());
  719. ImGui::Text("Screenshot automatic checks failed: %zu", m_wizardSettings.m_failedReports.size());
  720. for (const auto& failedReport : m_wizardSettings.m_failedReports)
  721. {
  722. const ScriptReporter::ReportIndex& reportIndex = failedReport.second;
  723. const ScriptReporter::ScriptReport& scriptReport = scriptReports[reportIndex.first];
  724. const ScriptReporter::ScreenshotTestInfo& screenshotTest = scriptReport.m_screenshotTests[reportIndex.second];
  725. ImGui::Text(
  726. "\t%s %s '%s' %f", scriptReport.m_scriptAssetPath.c_str(), screenshotTest.m_screenshotFilePath.c_str(),
  727. screenshotTest.m_toleranceLevel.m_name.c_str(), screenshotTest.m_officialComparisonResult.m_finalDiffScore);
  728. }
  729. // Present the information by printing highest differences first. See enum class ImageDifferenceLevel
  730. for (int i = aznumeric_cast<int>(imageDifferenceSummary.size() - 1); i >= 0; --i)
  731. {
  732. if (!imageDifferenceSummary[i].empty())
  733. {
  734. ImGui::Text("Screenshot interactive check '%s'", PrecommitWizardSettings::ManualInspectionDifferenceLevels[i]);
  735. for (const auto& reportIndex : imageDifferenceSummary[i])
  736. {
  737. const ScriptReporter::ScriptReport& scriptReport = scriptReports[reportIndex.first];
  738. const ScriptReporter::ScreenshotTestInfo& screenshotTest = scriptReport.m_screenshotTests[reportIndex.second];
  739. ImGui::Text(
  740. "\t%s %s '%s' %f", scriptReport.m_scriptAssetPath.c_str(), screenshotTest.m_screenshotFilePath.c_str(),
  741. screenshotTest.m_toleranceLevel.m_name.c_str(), screenshotTest.m_officialComparisonResult.m_finalDiffScore);
  742. }
  743. }
  744. }
  745. if (copyToClipboard)
  746. {
  747. ImGui::LogFinish();
  748. }
  749. ImGui::Separator();
  750. if (ImGui::Button("Back"))
  751. {
  752. m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ManualInspection;
  753. auto reportIndexToDifference = m_wizardSettings.m_reportIndexDifferenceLevelMap.find(m_wizardSettings.m_reportIterator->second);
  754. m_wizardSettings.m_inspectionSelection = reportIndexToDifference != m_wizardSettings.m_reportIndexDifferenceLevelMap.end()
  755. ? reportIndexToDifference->second
  756. : PrecommitWizardSettings::DefaultInspectionSelection;
  757. }
  758. }
  759. void ScriptManager::PrepareAndExecuteScript(const AZStd::string& scriptFilePath)
  760. {
  761. ReportScriptableAction(AZStd::string::format("RunScript('%s')", scriptFilePath.c_str()));
  762. // Save the window size so we can restore it after running the script, in case the script calls ResizeViewport
  763. AzFramework::NativeWindowHandle defaultWindowHandle;
  764. AzFramework::WindowSize windowSize;
  765. AzFramework::WindowSystemRequestBus::BroadcastResult(defaultWindowHandle, &AzFramework::WindowSystemRequestBus::Events::GetDefaultWindowHandle);
  766. AzFramework::WindowRequestBus::EventResult(windowSize, defaultWindowHandle, &AzFramework::WindowRequests::GetClientAreaSize);
  767. m_savedViewportWidth = windowSize.m_width;
  768. m_savedViewportHeight = windowSize.m_height;
  769. if (m_savedViewportWidth == 0 || m_savedViewportHeight == 0)
  770. {
  771. AZ_Assert(false, "Could not get current window size");
  772. }
  773. else
  774. {
  775. m_shouldRestoreViewportSize = true;
  776. }
  777. // Setup the ScriptReporter to track and report the results
  778. m_scriptReporter.Reset();
  779. m_scriptReporter.SetAvailableToleranceLevels(m_imageComparisonOptions.GetAvailableToleranceLevels());
  780. if (m_imageComparisonOptions.IsLevelAdjusted())
  781. {
  782. m_scriptReporter.SetInvalidationMessage("Results are invalid because the tolerance level has been adjusted.");
  783. }
  784. else if (!m_imageComparisonOptions.IsScriptControlled())
  785. {
  786. m_scriptReporter.SetInvalidationMessage("Results are invalid because the tolerance level has been overridden.");
  787. }
  788. else
  789. {
  790. m_scriptReporter.SetInvalidationMessage("");
  791. }
  792. AZ_Assert(m_executingScripts.empty(), "There should be no active scripts at this point");
  793. ExecuteScript(scriptFilePath);
  794. }
  795. void ScriptManager::ExecuteScript(const AZStd::string& scriptFilePath)
  796. {
  797. AZ::Data::Asset<AZ::ScriptAsset> scriptAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::ScriptAsset>(scriptFilePath.c_str());
  798. if (!scriptAsset)
  799. {
  800. // Push an error operation on the back of the queue instead of reporting it immediately so it doesn't get lost
  801. // in front of a bunch of queued m_scriptOperations.
  802. Script_Error(AZStd::string::format("Could not find or load script asset '%s'.", scriptFilePath.c_str()));
  803. return;
  804. }
  805. if (s_instance->m_executingScripts.find(scriptAsset.GetId()) != s_instance->m_executingScripts.end())
  806. {
  807. Script_Error(AZStd::string::format("Calling script '%s' would likely cause an infinite loop and crash. Skipping.", scriptFilePath.c_str()));
  808. return;
  809. }
  810. if (s_instance->m_imageComparisonOptions.IsScriptControlled())
  811. {
  812. s_instance->m_imageComparisonOptions.SelectToleranceLevel(nullptr); // Clear the preset before each script to make sure the script is selecting it.
  813. }
  814. // Execute(script) will add commands to the m_scriptOperations. These should be considered part of their own test script, for reporting purposes.
  815. s_instance->m_scriptOperations.push([scriptFilePath]()
  816. {
  817. s_instance->m_scriptReporter.PushScript(scriptFilePath);
  818. }
  819. );
  820. s_instance->m_scriptOperations.push([scriptFilePath]()
  821. {
  822. AZ_Printf("Automation", "Running script '%s'...\n", scriptFilePath.c_str());
  823. }
  824. );
  825. s_instance->m_executingScripts.insert(scriptAsset.GetId());
  826. if (!s_instance->m_scriptContext->Execute(scriptAsset->GetScriptBuffer().data(), scriptFilePath.c_str(), scriptAsset->GetScriptBuffer().size()))
  827. {
  828. // Push an error operation on the back of the queue instead of reporting it immediately so it doesn't get lost
  829. // in front of a bunch of queued m_scriptOperations.
  830. Script_Error(AZStd::string::format("Error running script '%s'.", scriptAsset.ToString<AZStd::string>().c_str()));
  831. }
  832. s_instance->m_executingScripts.erase(scriptAsset.GetId());
  833. // Execute(script) will have added commands to the m_scriptOperations. When they finish, consider this test as completed, for reporting purposes.
  834. s_instance->m_scriptOperations.push([]()
  835. {
  836. // We don't call m_scriptReporter.PopScript() yet because some cleanup needs to happen in TickScript() on the next frame.
  837. AZ_Assert(!s_instance->m_shouldPopScript, "m_shouldPopScript is already true");
  838. s_instance->m_shouldPopScript = true;
  839. }
  840. );
  841. }
  842. void ScriptManager::OnCameraMoveEnded(AZ::TypeId controllerTypeId, uint32_t channels)
  843. {
  844. if (controllerTypeId == azrtti_typeid<AZ::Debug::ArcBallControllerComponent>())
  845. {
  846. if (channels & AZ::Debug::ArcBallControllerChannel_Center)
  847. {
  848. AZ::Vector3 center = AZ::Vector3::CreateZero();
  849. AZ::Debug::ArcBallControllerRequestBus::EventResult(center, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetCenter);
  850. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetCenter(Vector3(%f, %f, %f))", (float)center.GetX(), (float)center.GetY(), (float)center.GetZ()));
  851. }
  852. if (channels & AZ::Debug::ArcBallControllerChannel_Pan)
  853. {
  854. AZ::Vector3 pan = AZ::Vector3::CreateZero();
  855. AZ::Debug::ArcBallControllerRequestBus::EventResult(pan, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetPan);
  856. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetPan(Vector3(%f, %f, %f))", (float)pan.GetX(), (float)pan.GetY(), (float)pan.GetZ()));
  857. }
  858. if (channels & AZ::Debug::ArcBallControllerChannel_Heading)
  859. {
  860. float heading = 0.0;
  861. AZ::Debug::ArcBallControllerRequestBus::EventResult(heading, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetHeading);
  862. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetHeading(DegToRad(%f))", AZ::RadToDeg(heading)));
  863. }
  864. if (channels & AZ::Debug::ArcBallControllerChannel_Pitch)
  865. {
  866. float pitch = 0.0;
  867. AZ::Debug::ArcBallControllerRequestBus::EventResult(pitch, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetPitch);
  868. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetPitch(DegToRad(%f))", AZ::RadToDeg(pitch)));
  869. }
  870. if (channels & AZ::Debug::ArcBallControllerChannel_Distance)
  871. {
  872. float distance = 0.0;
  873. AZ::Debug::ArcBallControllerRequestBus::EventResult(distance, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetDistance);
  874. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetDistance(%f)", distance));
  875. }
  876. }
  877. if (controllerTypeId == azrtti_typeid<AZ::Debug::NoClipControllerComponent>())
  878. {
  879. if (channels & AZ::Debug::NoClipControllerChannel_Position)
  880. {
  881. AZ::Vector3 position = AZ::Vector3::CreateZero();
  882. AZ::Debug::NoClipControllerRequestBus::EventResult(position, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetPosition);
  883. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetPosition(Vector3(%f, %f, %f))", (float)position.GetX(), (float)position.GetY(), (float)position.GetZ()));
  884. }
  885. if (channels & AZ::Debug::NoClipControllerChannel_Orientation)
  886. {
  887. float heading = 0.0;
  888. AZ::Debug::NoClipControllerRequestBus::EventResult(heading, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetHeading);
  889. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetHeading(DegToRad(%f))", AZ::RadToDeg(heading)));
  890. float pitch = 0.0;
  891. AZ::Debug::NoClipControllerRequestBus::EventResult(pitch, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetPitch);
  892. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetPitch(DegToRad(%f))", AZ::RadToDeg(pitch)));
  893. }
  894. if (channels & AZ::Debug::NoClipControllerChannel_Fov)
  895. {
  896. float fov = 0.0;
  897. AZ::Debug::NoClipControllerRequestBus::EventResult(fov, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetFov);
  898. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetFov(DegToRad(%f))", AZ::RadToDeg(fov)));
  899. }
  900. }
  901. }
  902. void ScriptManager::ReflectScriptContext(AZ::BehaviorContext* behaviorContext)
  903. {
  904. AZ::MathReflect(behaviorContext);
  905. // Utilities...
  906. behaviorContext->Method("RunScript", &Script_RunScript);
  907. behaviorContext->Method("Error", &Script_Error);
  908. behaviorContext->Method("Warning", &Script_Warning);
  909. behaviorContext->Method("Print", &Script_Print);
  910. behaviorContext->Method("IdleFrames", &Script_IdleFrames);
  911. behaviorContext->Method("IdleSeconds", &Script_IdleSeconds);
  912. behaviorContext->Method("LockFrameTime", &Script_LockFrameTime);
  913. behaviorContext->Method("UnlockFrameTime", &Script_UnlockFrameTime);
  914. behaviorContext->Method("ResizeViewport", &Script_ResizeViewport);
  915. behaviorContext->Method("SetShowImGui", &Script_SetShowImGui);
  916. behaviorContext->Method("ExecuteConsoleCommand", &Script_ExecuteConsoleCommand);
  917. // Utilities returning data (these special functions do return data because they don't read dynamic state)...
  918. behaviorContext->Method("ResolvePath", &Script_ResolvePath);
  919. behaviorContext->Method("NormalizePath", &Script_NormalizePath);
  920. behaviorContext->Method("DegToRad", &Script_DegToRad);
  921. behaviorContext->Method("GetRenderApiName", &Script_GetRenderApiName);
  922. behaviorContext->Method("GetRandomTestSeed", &Script_GetRandomTestSeed);
  923. // Samples...
  924. behaviorContext->Method("OpenSample", &Script_OpenSample);
  925. behaviorContext->Method("SetImguiValue", &Script_SetImguiValue);
  926. // Debug profilers...
  927. behaviorContext->Method("ShowTool", &Script_ShowTool);
  928. // Screenshots...
  929. behaviorContext->Method("SelectImageComparisonToleranceLevel", &Script_SelectImageComparisonToleranceLevel);
  930. behaviorContext->Method("CaptureScreenshot", &Script_CaptureScreenshot);
  931. behaviorContext->Method("CaptureScreenshotWithImGui", &Script_CaptureScreenshotWithImGui);
  932. behaviorContext->Method("CaptureScreenshotWithPreview", &Script_CaptureScreenshotWithPreview);
  933. behaviorContext->Method("CapturePassAttachment", &Script_CapturePassAttachment);
  934. // Profiling data...
  935. behaviorContext->Method("CapturePassTimestamp", &Script_CapturePassTimestamp);
  936. behaviorContext->Method("CaptureCpuFrameTime", &Script_CaptureCpuFrameTime);
  937. behaviorContext->Method("CapturePassPipelineStatistics", &Script_CapturePassPipelineStatistics);
  938. behaviorContext->Method("CaptureCpuProfilingStatistics", &Script_CaptureCpuProfilingStatistics);
  939. behaviorContext->Method("CaptureBenchmarkMetadata", &Script_CaptureBenchmarkMetadata);
  940. // Camera...
  941. behaviorContext->Method("ArcBallCameraController_SetCenter", &Script_ArcBallCameraController_SetCenter);
  942. behaviorContext->Method("ArcBallCameraController_SetPan", &Script_ArcBallCameraController_SetPan);
  943. behaviorContext->Method("ArcBallCameraController_SetDistance", &Script_ArcBallCameraController_SetDistance);
  944. behaviorContext->Method("ArcBallCameraController_SetHeading", &Script_ArcBallCameraController_SetHeading);
  945. behaviorContext->Method("ArcBallCameraController_SetPitch", &Script_ArcBallCameraController_SetPitch);
  946. behaviorContext->Method("NoClipCameraController_SetPosition", &Script_NoClipCameraController_SetPosition);
  947. behaviorContext->Method("NoClipCameraController_SetHeading", &Script_NoClipCameraController_SetHeading);
  948. behaviorContext->Method("NoClipCameraController_SetPitch", &Script_NoClipCameraController_SetPitch);
  949. behaviorContext->Method("NoClipCameraController_SetFov", &Script_NoClipCameraController_SetFov);
  950. // Asset System...
  951. AZ::BehaviorParameterOverrides expectedCountDetails = {"expectedCount", "Expected number of asset jobs; default=1", aznew AZ::BehaviorDefaultValue(1u)};
  952. const AZStd::array<AZ::BehaviorParameterOverrides, 2> assetTrackingExpectAssetArgs = {{ AZ::BehaviorParameterOverrides{}, expectedCountDetails }};
  953. behaviorContext->Method("AssetTracking_Start", &Script_AssetTracking_Start);
  954. behaviorContext->Method("AssetTracking_ExpectAsset", &Script_AssetTracking_ExpectAsset, assetTrackingExpectAssetArgs);
  955. behaviorContext->Method("AssetTracking_IdleUntilExpectedAssetsFinish", &Script_AssetTracking_IdleUntilExpectedAssetsFinish);
  956. behaviorContext->Method("AssetTracking_Stop", &Script_AssetTracking_Stop);
  957. }
  958. void ScriptManager::Script_Error(const AZStd::string& message)
  959. {
  960. auto func = [message]()
  961. {
  962. ReportScriptError(message.c_str());
  963. };
  964. s_instance->m_scriptOperations.push(AZStd::move(func));
  965. }
  966. void ScriptManager::Script_Warning(const AZStd::string& message)
  967. {
  968. auto func = [message]()
  969. {
  970. ReportScriptWarning(message.c_str());
  971. };
  972. s_instance->m_scriptOperations.push(AZStd::move(func));
  973. }
  974. void ScriptManager::Script_Print(const AZStd::string& message)
  975. {
  976. auto func = [message]()
  977. {
  978. AZ_TracePrintf("Automation", "Script: %s\n", message.c_str());
  979. };
  980. s_instance->m_scriptOperations.push(AZStd::move(func));
  981. }
  982. AZStd::string ScriptManager::Script_ResolvePath(const AZStd::string& path)
  983. {
  984. return Utils::ResolvePath(path);
  985. }
  986. AZStd::string ScriptManager::Script_NormalizePath(const AZStd::string& path)
  987. {
  988. AZStd::string normalizedPath = path;
  989. AzFramework::StringFunc::Path::Normalize(normalizedPath);
  990. return normalizedPath;
  991. }
  992. void ScriptManager::Script_OpenSample(const AZStd::string& sampleName)
  993. {
  994. auto operation = [sampleName]()
  995. {
  996. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  997. if (sampleName.empty())
  998. {
  999. SampleComponentManagerRequestBus::Broadcast(&SampleComponentManagerRequests::Reset);
  1000. }
  1001. else
  1002. {
  1003. bool foundSample = false;
  1004. SampleComponentManagerRequestBus::BroadcastResult(foundSample, &SampleComponentManagerRequests::OpenSample, sampleName);
  1005. if (foundSample)
  1006. {
  1007. // Samples need a few frames to initialize before consuming actions from ScriptableImGui,
  1008. // so we need to wait before letting the script schedule ScriptableImGui actions.
  1009. // They need 1 frame to activate, 1 frame to start ticking, and 1 frame to guarantee
  1010. // that a sample OnTick occurs before a ScriptManager::OnTick. We schedule
  1011. // a few extra just in case.
  1012. AZ_Assert(s_instance->m_scriptIdleFrames == 0, "m_scriptIdleFrames is being stomped");
  1013. s_instance->m_scriptIdleFrames = 6;
  1014. }
  1015. }
  1016. };
  1017. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1018. }
  1019. void ScriptManager::Script_ShowTool(const AZStd::string& toolName, bool enable)
  1020. {
  1021. auto operation = [toolName, enable]()
  1022. {
  1023. bool foundTool = false;
  1024. SampleComponentManagerRequestBus::BroadcastResult(foundTool, &SampleComponentManagerRequests::ShowTool, toolName, enable);
  1025. AZ_Warning("ScriptManager", foundTool, "Can't find [%s] tool", toolName.c_str());
  1026. };
  1027. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1028. }
  1029. void ScriptManager::Script_RunScript(const AZStd::string& scriptFilePath)
  1030. {
  1031. // Unlike other Script_ callback functions, we process immediately instead of pushing onto the m_scriptOperations queue.
  1032. // This function is special because running the script is what adds more commands onto the m_scriptOperations queue.
  1033. s_instance->ExecuteScript(scriptFilePath);
  1034. }
  1035. void ScriptManager::Script_IdleFrames(int numFrames)
  1036. {
  1037. auto operation = [numFrames]()
  1038. {
  1039. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1040. AZ_Assert(s_instance->m_scriptIdleFrames == 0, "m_scriptIdleFrames is being stomped");
  1041. s_instance->m_scriptIdleFrames = numFrames;
  1042. };
  1043. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1044. }
  1045. void ScriptManager::Script_IdleSeconds(float numSeconds)
  1046. {
  1047. auto operation = [numSeconds]()
  1048. {
  1049. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1050. s_instance->m_scriptIdleSeconds = numSeconds;
  1051. };
  1052. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1053. }
  1054. void ScriptManager::Script_LockFrameTime(float seconds)
  1055. {
  1056. auto operation = [seconds]()
  1057. {
  1058. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1059. int milliseconds = static_cast<int>(seconds * 1000);
  1060. AZ::Interface<AZ::IConsole>::Get()->PerformCommand(AZStd::string::format("t_simulationTickDeltaOverride %d", milliseconds).c_str());
  1061. s_instance->m_frameTimeIsLocked = true;
  1062. };
  1063. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1064. }
  1065. void ScriptManager::Script_UnlockFrameTime()
  1066. {
  1067. auto operation = []()
  1068. {
  1069. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1070. AZ::Interface<AZ::IConsole>::Get()->PerformCommand("t_simulationTickDeltaOverride 0");
  1071. s_instance->m_frameTimeIsLocked = false;
  1072. };
  1073. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1074. }
  1075. void ScriptManager::Script_SetImguiValue(AZ::ScriptDataContext& dc)
  1076. {
  1077. if (dc.GetNumArguments() != 2)
  1078. {
  1079. ReportScriptError("Wrong number of arguments for SetImguiValue");
  1080. return;
  1081. }
  1082. if (!dc.IsString(0))
  1083. {
  1084. ReportScriptError("SetImguiValue first argument must be a string");
  1085. return;
  1086. }
  1087. const char* fieldName = nullptr;
  1088. dc.ReadArg(0, fieldName);
  1089. AZStd::string fieldNameString = fieldName; // Because the lambda will need to capture a copy of something, not a pointer
  1090. if (dc.IsBoolean(1))
  1091. {
  1092. bool value = false;
  1093. dc.ReadArg(1, value);
  1094. auto func = [fieldNameString, value]()
  1095. {
  1096. ScriptableImGui::SetBool(fieldNameString, value);
  1097. };
  1098. s_instance->m_scriptOperations.push(AZStd::move(func));
  1099. }
  1100. else if (dc.IsNumber(1))
  1101. {
  1102. float value = 0.0f;
  1103. dc.ReadArg(1, value);
  1104. auto func = [fieldNameString, value]()
  1105. {
  1106. ScriptableImGui::SetNumber(fieldNameString, value);
  1107. };
  1108. s_instance->m_scriptOperations.push(AZStd::move(func));
  1109. }
  1110. else if (dc.IsString(1))
  1111. {
  1112. const char* value = nullptr;
  1113. dc.ReadArg(1, value);
  1114. AZStd::string valueString = value; // Because the lambda will need to capture a copy of something, not a pointer
  1115. auto func = [fieldNameString, valueString]()
  1116. {
  1117. ScriptableImGui::SetString(fieldNameString, valueString);
  1118. };
  1119. s_instance->m_scriptOperations.push(AZStd::move(func));
  1120. }
  1121. else if (dc.IsClass<AZ::Vector3>(1))
  1122. {
  1123. AZ::Vector3 value = AZ::Vector3::CreateZero();
  1124. dc.ReadArg(1, value);
  1125. auto func = [fieldNameString, value]()
  1126. {
  1127. ScriptableImGui::SetVector(fieldNameString, value);
  1128. };
  1129. s_instance->m_scriptOperations.push(AZStd::move(func));
  1130. }
  1131. else if (dc.IsClass<AZ::Vector2>(1))
  1132. {
  1133. AZ::Vector2 value = AZ::Vector2::CreateZero();
  1134. dc.ReadArg(1, value);
  1135. auto func = [fieldNameString, value]()
  1136. {
  1137. ScriptableImGui::SetVector(fieldNameString, value);
  1138. };
  1139. s_instance->m_scriptOperations.push(AZStd::move(func));
  1140. }
  1141. }
  1142. void ScriptManager::Script_ResizeViewport(int width, int height)
  1143. {
  1144. auto operation = [width,height]()
  1145. {
  1146. if (Utils::SupportsResizeClientArea())
  1147. {
  1148. Utils::ResizeClientArea(width, height);
  1149. }
  1150. else
  1151. {
  1152. s_instance->ReportScriptError("ResizeViewport() is not supported on this platform");
  1153. }
  1154. };
  1155. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1156. }
  1157. void ScriptManager::Script_ExecuteConsoleCommand(const AZStd::string& command)
  1158. {
  1159. auto operation = [command]()
  1160. {
  1161. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, command.c_str());
  1162. };
  1163. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1164. }
  1165. void ScriptManager::SetShowImGui(bool show)
  1166. {
  1167. m_prevShowImGui = m_showImGui;
  1168. if (show)
  1169. {
  1170. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::ShowAllImGuiPasses);
  1171. }
  1172. else
  1173. {
  1174. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::HideAllImGuiPasses);
  1175. }
  1176. m_showImGui = show;
  1177. }
  1178. void ScriptManager::Script_SetShowImGui(bool show)
  1179. {
  1180. auto operation = [show]()
  1181. {
  1182. s_instance->SetShowImGui(show);
  1183. };
  1184. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1185. }
  1186. bool ScriptManager::PrepareForScreenCapture(const AZStd::string& path)
  1187. {
  1188. if (!Utils::IsFileUnderFolder(Utils::ResolvePath(path), ScreenshotPaths::GetScreenshotsFolder(true)))
  1189. {
  1190. // 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.
  1191. ReportScriptError(AZStd::string::format(
  1192. "Screenshots must be captured under the '%s' folder. Attempted to save screenshot to '%s'.",
  1193. ScreenshotPaths::GetScreenshotsFolder(false).c_str(), path.c_str()));
  1194. return false;
  1195. }
  1196. // 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.
  1197. if (AZ::IO::LocalFileIO::GetInstance()->Exists(path.c_str()) && !AZ::IO::LocalFileIO::GetInstance()->Remove(path.c_str()))
  1198. {
  1199. ReportScriptError(AZStd::string::format("Failed to delete existing screenshot file '%s'.", path.c_str()));
  1200. return false;
  1201. }
  1202. s_instance->m_scriptReporter.AddScreenshotTest(path);
  1203. s_instance->m_isCapturePending = true;
  1204. s_instance->AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect();
  1205. s_instance->PauseScript();
  1206. return true;
  1207. }
  1208. void ScriptManager::Script_SelectImageComparisonToleranceLevel(const AZStd::string& presetName)
  1209. {
  1210. auto operation = [presetName]()
  1211. {
  1212. s_instance->m_imageComparisonOptions.SelectToleranceLevel(presetName);
  1213. };
  1214. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1215. }
  1216. void ScriptManager::Script_CaptureScreenshot(const AZStd::string& filePath)
  1217. {
  1218. Script_SetShowImGui(false);
  1219. auto operation = [filePath]()
  1220. {
  1221. // Note this will pause the script until the capture is complete
  1222. if (PrepareForScreenCapture(filePath))
  1223. {
  1224. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, filePath);
  1225. }
  1226. };
  1227. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1228. s_instance->m_scriptOperations.push([]()
  1229. {
  1230. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  1231. });
  1232. // restore imgui show/hide
  1233. s_instance->m_scriptOperations.push([]()
  1234. {
  1235. s_instance->SetShowImGui(s_instance->m_prevShowImGui);
  1236. });
  1237. }
  1238. void ScriptManager::Script_CaptureScreenshotWithImGui(const AZStd::string& filePath)
  1239. {
  1240. Script_SetShowImGui(true);
  1241. auto operation = [filePath]()
  1242. {
  1243. // Note this will pause the script until the capture is complete
  1244. if (PrepareForScreenCapture(filePath))
  1245. {
  1246. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, filePath);
  1247. }
  1248. };
  1249. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1250. s_instance->m_scriptOperations.push([]()
  1251. {
  1252. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  1253. });
  1254. // restore imgui show/hide
  1255. s_instance->m_scriptOperations.push([]()
  1256. {
  1257. s_instance->SetShowImGui(s_instance->m_prevShowImGui);
  1258. });
  1259. }
  1260. void ScriptManager::Script_CaptureScreenshotWithPreview(const AZStd::string& filePath)
  1261. {
  1262. auto operation = [filePath]()
  1263. {
  1264. // Note this will pause the script until the capture is complete
  1265. if (PrepareForScreenCapture(filePath))
  1266. {
  1267. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshotWithPreview, filePath);
  1268. }
  1269. };
  1270. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1271. s_instance->m_scriptOperations.push([]()
  1272. {
  1273. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  1274. });
  1275. }
  1276. void ScriptManager::Script_CapturePassAttachment(AZ::ScriptDataContext& dc)
  1277. {
  1278. if (dc.GetNumArguments() != 3 && dc.GetNumArguments() != 4)
  1279. {
  1280. ReportScriptError("CapturePassAttachment needs three or four arguments");
  1281. return;
  1282. }
  1283. if (!dc.IsTable(0))
  1284. {
  1285. ReportScriptError("CapturePassAttachment's first argument must be a table of strings");
  1286. return;
  1287. }
  1288. if (!dc.IsString(1) || !dc.IsString(2))
  1289. {
  1290. ReportScriptError("CapturePassAttachment's second and third argument must be strings");
  1291. return;
  1292. }
  1293. if (dc.GetNumArguments() == 4 && !dc.IsString(3))
  1294. {
  1295. ReportScriptError("CapturePassAttachment's forth argument must be a string 'Input' or 'Output'");
  1296. return;
  1297. }
  1298. const char* stringValue = nullptr;
  1299. AZStd::vector<AZStd::string> passHierarchy;
  1300. AZStd::string slot;
  1301. AZStd::string outputFilePath;
  1302. // read slot name and output file path
  1303. dc.ReadArg(1, stringValue);
  1304. slot = AZStd::string(stringValue);
  1305. dc.ReadArg(2, stringValue);
  1306. outputFilePath = AZStd::string(stringValue);
  1307. AZ::RPI::PassAttachmentReadbackOption readbackOption = AZ::RPI::PassAttachmentReadbackOption::Output;
  1308. if (dc.GetNumArguments() == 4)
  1309. {
  1310. AZStd::string option;
  1311. dc.ReadArg(3, option);
  1312. if (option == "Input")
  1313. {
  1314. readbackOption = AZ::RPI::PassAttachmentReadbackOption::Input;
  1315. }
  1316. }
  1317. // read pass hierarchy
  1318. AZ::ScriptDataContext stringtable;
  1319. dc.InspectTable(0, stringtable);
  1320. const char* fieldName;
  1321. int fieldIndex;
  1322. int elementIndex;
  1323. while (stringtable.InspectNextElement(elementIndex, fieldName, fieldIndex))
  1324. {
  1325. if (fieldIndex != -1)
  1326. {
  1327. if (!stringtable.IsString(elementIndex))
  1328. {
  1329. ReportScriptError("CapturePassAttachment's first argument must contain only strings");
  1330. return;
  1331. }
  1332. const char* stringTableValue = nullptr;
  1333. if (stringtable.ReadValue(elementIndex, stringTableValue))
  1334. {
  1335. passHierarchy.push_back(stringTableValue);
  1336. }
  1337. }
  1338. }
  1339. auto operation = [passHierarchy, slot, outputFilePath, readbackOption]()
  1340. {
  1341. // Note this will pause the script until the capture is complete
  1342. if (PrepareForScreenCapture(outputFilePath))
  1343. {
  1344. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CapturePassAttachment, passHierarchy, slot, outputFilePath, readbackOption);
  1345. }
  1346. };
  1347. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1348. s_instance->m_scriptOperations.push([]()
  1349. {
  1350. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  1351. });
  1352. }
  1353. void ScriptManager::OnCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string &info)
  1354. {
  1355. m_isCapturePending = false;
  1356. AZ::Render::FrameCaptureNotificationBus::Handler::BusDisconnect();
  1357. ResumeScript();
  1358. // 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
  1359. // alert users to a common issue and what action to take. Any other Format issues will be reported by FrameCaptureSystemComponent with a
  1360. // "Can't save image with format %s to a ppm file" message.
  1361. if (result == AZ::Render::FrameCaptureResult::UnsupportedFormat && info.find(AZ::RHI::ToString(AZ::RHI::Format::R10G10B10A2_UNORM)) != AZStd::string::npos)
  1362. {
  1363. 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.");
  1364. AbortScripts("Script(s) aborted due to HDR configuration.");
  1365. }
  1366. }
  1367. void ScriptManager::OnCaptureQueryTimestampFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1368. {
  1369. m_isCapturePending = false;
  1370. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1371. ResumeScript();
  1372. }
  1373. void ScriptManager::OnCaptureCpuFrameTimeFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1374. {
  1375. m_isCapturePending = false;
  1376. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1377. ResumeScript();
  1378. }
  1379. void ScriptManager::OnCaptureQueryPipelineStatisticsFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1380. {
  1381. m_isCapturePending = false;
  1382. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1383. ResumeScript();
  1384. }
  1385. void ScriptManager::OnCaptureFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1386. {
  1387. m_isCapturePending = false;
  1388. AZ::Debug::ProfilerNotificationBus::Handler::BusDisconnect();
  1389. ResumeScript();
  1390. }
  1391. void ScriptManager::OnCaptureBenchmarkMetadataFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1392. {
  1393. m_isCapturePending = false;
  1394. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1395. ResumeScript();
  1396. }
  1397. void ScriptManager::Script_CapturePassTimestamp(AZ::ScriptDataContext& dc)
  1398. {
  1399. AZStd::string outputFilePath;
  1400. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1401. if (!readScriptDataContext)
  1402. {
  1403. return;
  1404. }
  1405. auto operation = [outputFilePath]()
  1406. {
  1407. s_instance->m_isCapturePending = true;
  1408. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1409. s_instance->PauseScript();
  1410. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassTimestamp, outputFilePath);
  1411. };
  1412. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1413. }
  1414. void ScriptManager::Script_CaptureCpuFrameTime(AZ::ScriptDataContext& dc)
  1415. {
  1416. AZStd::string outputFilePath;
  1417. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1418. if (!readScriptDataContext)
  1419. {
  1420. return;
  1421. }
  1422. auto operation = [outputFilePath]()
  1423. {
  1424. s_instance->m_isCapturePending = true;
  1425. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1426. s_instance->PauseScript();
  1427. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CaptureCpuFrameTime, outputFilePath);
  1428. };
  1429. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1430. }
  1431. void ScriptManager::Script_CapturePassPipelineStatistics(AZ::ScriptDataContext& dc)
  1432. {
  1433. AZStd::string outputFilePath;
  1434. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1435. if (!readScriptDataContext)
  1436. {
  1437. return;
  1438. }
  1439. auto operation = [outputFilePath]()
  1440. {
  1441. s_instance->m_isCapturePending = true;
  1442. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1443. s_instance->PauseScript();
  1444. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassPipelineStatistics, outputFilePath);
  1445. };
  1446. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1447. }
  1448. void ScriptManager::Script_CaptureCpuProfilingStatistics(AZ::ScriptDataContext& dc)
  1449. {
  1450. AZStd::string outputFilePath;
  1451. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1452. if (!readScriptDataContext)
  1453. {
  1454. return;
  1455. }
  1456. auto operation = [outputFilePath]()
  1457. {
  1458. s_instance->m_isCapturePending = true;
  1459. s_instance->AZ::Debug::ProfilerNotificationBus::Handler::BusConnect();
  1460. s_instance->PauseScript();
  1461. if (auto profilerSystem = AZ::Debug::ProfilerSystemInterface::Get(); profilerSystem)
  1462. {
  1463. profilerSystem->CaptureFrame(outputFilePath);
  1464. }
  1465. };
  1466. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1467. }
  1468. void ScriptManager::Script_CaptureBenchmarkMetadata(AZ::ScriptDataContext& dc)
  1469. {
  1470. if (dc.GetNumArguments() != 2)
  1471. {
  1472. ReportScriptError("CaptureBenchmarkMetadata needs two arguments, benchmarkName and outputFilePath.");
  1473. return;
  1474. }
  1475. if (!dc.IsString(0) || !dc.IsString(1))
  1476. {
  1477. ReportScriptError("CaptureBenchmarkMetadata's arguments benchmarkName and outputFilePath must both be of type string.");
  1478. return;
  1479. }
  1480. const char* stringValue = nullptr;
  1481. AZStd::string benchmarkName;
  1482. AZStd::string outputFilePath;
  1483. dc.ReadArg(0, stringValue);
  1484. benchmarkName = AZStd::string(stringValue);
  1485. dc.ReadArg(1, stringValue);
  1486. outputFilePath = AZStd::string(stringValue);
  1487. auto operation = [benchmarkName, outputFilePath]()
  1488. {
  1489. s_instance->m_isCapturePending = true;
  1490. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1491. s_instance->PauseScript();
  1492. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CaptureBenchmarkMetadata, benchmarkName, outputFilePath);
  1493. };
  1494. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1495. }
  1496. bool ScriptManager::ValidateProfilingCaptureScripContexts(AZ::ScriptDataContext& dc, AZStd::string& outputFilePath)
  1497. {
  1498. if (dc.GetNumArguments() != 1)
  1499. {
  1500. ReportScriptError("ProfilingCaptureScriptDataContext needs one argument");
  1501. return false;
  1502. }
  1503. if (!dc.IsString(0))
  1504. {
  1505. ReportScriptError("ProfilingCaptureScriptDataContext's first (and only) argument must be of type string");
  1506. return false;
  1507. }
  1508. // Read slot name and output file path
  1509. const char* stringValue = nullptr;
  1510. dc.ReadArg(0, stringValue);
  1511. if (stringValue == nullptr)
  1512. {
  1513. ReportScriptError("ProfilingCaptureScriptDataContext failed to read the string value");
  1514. return false;
  1515. }
  1516. outputFilePath = AZStd::string(stringValue);
  1517. return true;
  1518. }
  1519. float ScriptManager::Script_DegToRad(float degrees)
  1520. {
  1521. return AZ::DegToRad(degrees);
  1522. }
  1523. AZStd::string ScriptManager::Script_GetRenderApiName()
  1524. {
  1525. AZ::RPI::RPISystemInterface* rpiSystem = AZ::RPI::RPISystemInterface::Get();
  1526. return rpiSystem->GetRenderApiName().GetCStr();
  1527. }
  1528. int ScriptManager::Script_GetRandomTestSeed()
  1529. {
  1530. return s_instance->m_testSuiteRunConfig.m_randomSeed;
  1531. }
  1532. void ScriptManager::CheckArcBallControllerHandler()
  1533. {
  1534. if (0 == AZ::Debug::ArcBallControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
  1535. {
  1536. ReportScriptError("There is no handler for ArcBallControllerRequestBus for the camera entity.");
  1537. }
  1538. }
  1539. void ScriptManager::CheckNoClipControllerHandler()
  1540. {
  1541. if (0 == AZ::Debug::NoClipControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
  1542. {
  1543. ReportScriptError("There is no handler for NoClipControllerRequestBus for the camera entity.");
  1544. }
  1545. }
  1546. void ScriptManager::Script_ArcBallCameraController_SetCenter(AZ::Vector3 center)
  1547. {
  1548. auto operation = [center]()
  1549. {
  1550. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1551. CheckArcBallControllerHandler();
  1552. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  1553. };
  1554. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1555. }
  1556. void ScriptManager::Script_ArcBallCameraController_SetPan(AZ::Vector3 pan)
  1557. {
  1558. auto operation = [pan]()
  1559. {
  1560. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1561. CheckArcBallControllerHandler();
  1562. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPan, pan);
  1563. };
  1564. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1565. }
  1566. void ScriptManager::Script_ArcBallCameraController_SetDistance(float distance)
  1567. {
  1568. auto operation = [distance]()
  1569. {
  1570. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1571. CheckArcBallControllerHandler();
  1572. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, distance);
  1573. };
  1574. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1575. }
  1576. void ScriptManager::Script_ArcBallCameraController_SetHeading(float heading)
  1577. {
  1578. auto operation = [heading]()
  1579. {
  1580. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1581. CheckArcBallControllerHandler();
  1582. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading, heading);
  1583. };
  1584. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1585. }
  1586. void ScriptManager::Script_ArcBallCameraController_SetPitch(float pitch)
  1587. {
  1588. auto operation = [pitch]()
  1589. {
  1590. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1591. CheckArcBallControllerHandler();
  1592. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch, pitch);
  1593. };
  1594. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1595. }
  1596. void ScriptManager::Script_NoClipCameraController_SetPosition(AZ::Vector3 position)
  1597. {
  1598. auto operation = [position]()
  1599. {
  1600. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1601. CheckNoClipControllerHandler();
  1602. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPosition, position);
  1603. };
  1604. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1605. }
  1606. void ScriptManager::Script_NoClipCameraController_SetHeading(float heading)
  1607. {
  1608. auto operation = [heading]()
  1609. {
  1610. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1611. CheckNoClipControllerHandler();
  1612. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading, heading);
  1613. };
  1614. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1615. }
  1616. void ScriptManager::Script_NoClipCameraController_SetPitch(float pitch)
  1617. {
  1618. auto operation = [pitch]()
  1619. {
  1620. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1621. CheckNoClipControllerHandler();
  1622. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch, pitch);
  1623. };
  1624. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1625. }
  1626. void ScriptManager::Script_NoClipCameraController_SetFov(float fov)
  1627. {
  1628. auto operation = [fov]()
  1629. {
  1630. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1631. CheckNoClipControllerHandler();
  1632. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetFov, fov);
  1633. };
  1634. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1635. }
  1636. void ScriptManager::Script_AssetTracking_Start()
  1637. {
  1638. auto operation = []()
  1639. {
  1640. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1641. s_instance->m_assetStatusTracker.StartTracking();
  1642. };
  1643. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1644. }
  1645. void ScriptManager::Script_AssetTracking_ExpectAsset(const AZStd::string& sourceAssetPath, uint32_t expectedCount)
  1646. {
  1647. auto operation = [sourceAssetPath, expectedCount]()
  1648. {
  1649. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1650. s_instance->m_assetStatusTracker.ExpectAsset(sourceAssetPath, expectedCount);
  1651. };
  1652. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1653. }
  1654. void ScriptManager::Script_AssetTracking_IdleUntilExpectedAssetsFinish(float timeout)
  1655. {
  1656. auto operation = [timeout]()
  1657. {
  1658. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1659. AZ_Assert(!s_instance->m_waitForAssetTracker, "It shouldn't be possible to run the next command until m_waitForAssetTracker is false");
  1660. s_instance->m_waitForAssetTracker = true;
  1661. s_instance->m_assetTrackingTimeout = timeout;
  1662. };
  1663. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1664. }
  1665. void ScriptManager::Script_AssetTracking_Stop()
  1666. {
  1667. auto operation = []()
  1668. {
  1669. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1670. s_instance->m_assetStatusTracker.StopTracking();
  1671. };
  1672. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1673. }
  1674. } // namespace AtomSampleViewer