ScriptManager.cpp 59 KB

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