ScriptManager.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  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 && dc.GetNumArguments() != 4)
  966. {
  967. ReportScriptError("CapturePassAttachment needs three or four 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. if (dc.GetNumArguments() == 4 && !dc.IsString(3))
  981. {
  982. ReportScriptError("CapturePassAttachment's forth argument must be a string 'Input' or 'Output'");
  983. return;
  984. }
  985. const char* stringValue = nullptr;
  986. AZStd::vector<AZStd::string> passHierarchy;
  987. AZStd::string slot;
  988. AZStd::string outputFilePath;
  989. // read slot name and output file path
  990. dc.ReadArg(1, stringValue);
  991. slot = AZStd::string(stringValue);
  992. dc.ReadArg(2, stringValue);
  993. outputFilePath = AZStd::string(stringValue);
  994. AZ::RPI::PassAttachmentReadbackOption readbackOption = AZ::RPI::PassAttachmentReadbackOption::Output;
  995. if (dc.GetNumArguments() == 4)
  996. {
  997. AZStd::string option;
  998. dc.ReadArg(3, option);
  999. if (option == "Input")
  1000. {
  1001. readbackOption = AZ::RPI::PassAttachmentReadbackOption::Input;
  1002. }
  1003. }
  1004. // read pass hierarchy
  1005. AZ::ScriptDataContext stringtable;
  1006. dc.InspectTable(0, stringtable);
  1007. const char* fieldName;
  1008. int fieldIndex;
  1009. int elementIndex;
  1010. while (stringtable.InspectNextElement(elementIndex, fieldName, fieldIndex))
  1011. {
  1012. if (fieldIndex != -1)
  1013. {
  1014. if (!stringtable.IsString(elementIndex))
  1015. {
  1016. ReportScriptError("CapturePassAttachment's first argument must contain only strings");
  1017. return;
  1018. }
  1019. const char* stringTableValue = nullptr;
  1020. if (stringtable.ReadValue(elementIndex, stringTableValue))
  1021. {
  1022. passHierarchy.push_back(stringTableValue);
  1023. }
  1024. }
  1025. }
  1026. auto operation = [passHierarchy, slot, outputFilePath, readbackOption]()
  1027. {
  1028. // Note this will pause the script until the capture is complete
  1029. if (PrepareForScreenCapture(outputFilePath))
  1030. {
  1031. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CapturePassAttachment, passHierarchy, slot, outputFilePath, readbackOption);
  1032. }
  1033. };
  1034. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1035. s_instance->m_scriptOperations.push([]()
  1036. {
  1037. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  1038. });
  1039. }
  1040. void ScriptManager::OnCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string &info)
  1041. {
  1042. m_isCapturePending = false;
  1043. AZ::Render::FrameCaptureNotificationBus::Handler::BusDisconnect();
  1044. ResumeScript();
  1045. // 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
  1046. // alert users to a common issue and what action to take. Any other Format issues will be reported by FrameCaptureSystemComponent with a
  1047. // "Can't save image with format %s to a ppm file" message.
  1048. if (result == AZ::Render::FrameCaptureResult::UnsupportedFormat && info.find(AZ::RHI::ToString(AZ::RHI::Format::R10G10B10A2_UNORM)) != AZStd::string::npos)
  1049. {
  1050. 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.");
  1051. AbortScripts("Script(s) aborted due to HDR configuration.");
  1052. }
  1053. }
  1054. void ScriptManager::OnCaptureQueryTimestampFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1055. {
  1056. m_isCapturePending = false;
  1057. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1058. ResumeScript();
  1059. }
  1060. void ScriptManager::OnCaptureQueryPipelineStatisticsFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1061. {
  1062. m_isCapturePending = false;
  1063. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1064. ResumeScript();
  1065. }
  1066. void ScriptManager::OnCaptureCpuProfilingStatisticsFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1067. {
  1068. m_isCapturePending = false;
  1069. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1070. ResumeScript();
  1071. }
  1072. void ScriptManager::Script_CapturePassTimestamp(AZ::ScriptDataContext& dc)
  1073. {
  1074. AZStd::string outputFilePath;
  1075. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1076. if (!readScriptDataContext)
  1077. {
  1078. return;
  1079. }
  1080. auto operation = [outputFilePath]()
  1081. {
  1082. s_instance->m_isCapturePending = true;
  1083. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1084. s_instance->PauseScript();
  1085. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassTimestamp, outputFilePath);
  1086. };
  1087. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1088. }
  1089. void ScriptManager::Script_CapturePassPipelineStatistics(AZ::ScriptDataContext& dc)
  1090. {
  1091. AZStd::string outputFilePath;
  1092. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1093. if (!readScriptDataContext)
  1094. {
  1095. return;
  1096. }
  1097. auto operation = [outputFilePath]()
  1098. {
  1099. s_instance->m_isCapturePending = true;
  1100. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1101. s_instance->PauseScript();
  1102. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassPipelineStatistics, outputFilePath);
  1103. };
  1104. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1105. }
  1106. void ScriptManager::Script_CaptureCpuProfilingStatistics(AZ::ScriptDataContext& dc)
  1107. {
  1108. AZStd::string outputFilePath;
  1109. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1110. if (!readScriptDataContext)
  1111. {
  1112. return;
  1113. }
  1114. auto operation = [outputFilePath]()
  1115. {
  1116. s_instance->m_isCapturePending = true;
  1117. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1118. s_instance->PauseScript();
  1119. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CaptureCpuProfilingStatistics, outputFilePath);
  1120. };
  1121. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1122. }
  1123. bool ScriptManager::ValidateProfilingCaptureScripContexts(AZ::ScriptDataContext& dc, AZStd::string& outputFilePath)
  1124. {
  1125. if (dc.GetNumArguments() != 1)
  1126. {
  1127. ReportScriptError("ProfilingCaptureScriptDataContext needs one argument");
  1128. return false;
  1129. }
  1130. if (!dc.IsString(0))
  1131. {
  1132. ReportScriptError("ProfilingCaptureScriptDataContext's first (and only) argument must be of type string");
  1133. return false;
  1134. }
  1135. // Read slot name and output file path
  1136. const char* stringValue = nullptr;
  1137. dc.ReadArg(0, stringValue);
  1138. if (stringValue == nullptr)
  1139. {
  1140. ReportScriptError("ProfilingCaptureScriptDataContext failed to read the string value");
  1141. return false;
  1142. }
  1143. outputFilePath = AZStd::string(stringValue);
  1144. return true;
  1145. }
  1146. float ScriptManager::Script_DegToRad(float degrees)
  1147. {
  1148. return AZ::DegToRad(degrees);
  1149. }
  1150. AZStd::string ScriptManager::Script_GetRenderApiName()
  1151. {
  1152. AZ::RPI::RPISystemInterface* rpiSystem = AZ::RPI::RPISystemInterface::Get();
  1153. return rpiSystem->GetRenderApiName().GetCStr();
  1154. }
  1155. int ScriptManager::Script_GetRandomTestSeed()
  1156. {
  1157. return s_instance->m_testSuiteRunConfig.m_randomSeed;
  1158. }
  1159. void ScriptManager::CheckArcBallControllerHandler()
  1160. {
  1161. if (0 == AZ::Debug::ArcBallControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
  1162. {
  1163. ReportScriptError("There is no handler for ArcBallControllerRequestBus for the camera entity.");
  1164. }
  1165. }
  1166. void ScriptManager::CheckNoClipControllerHandler()
  1167. {
  1168. if (0 == AZ::Debug::NoClipControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
  1169. {
  1170. ReportScriptError("There is no handler for NoClipControllerRequestBus for the camera entity.");
  1171. }
  1172. }
  1173. void ScriptManager::Script_ArcBallCameraController_SetCenter(AZ::Vector3 center)
  1174. {
  1175. auto operation = [center]()
  1176. {
  1177. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1178. CheckArcBallControllerHandler();
  1179. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  1180. };
  1181. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1182. }
  1183. void ScriptManager::Script_ArcBallCameraController_SetPan(AZ::Vector3 pan)
  1184. {
  1185. auto operation = [pan]()
  1186. {
  1187. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1188. CheckArcBallControllerHandler();
  1189. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPan, pan);
  1190. };
  1191. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1192. }
  1193. void ScriptManager::Script_ArcBallCameraController_SetDistance(float distance)
  1194. {
  1195. auto operation = [distance]()
  1196. {
  1197. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1198. CheckArcBallControllerHandler();
  1199. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, distance);
  1200. };
  1201. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1202. }
  1203. void ScriptManager::Script_ArcBallCameraController_SetHeading(float heading)
  1204. {
  1205. auto operation = [heading]()
  1206. {
  1207. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1208. CheckArcBallControllerHandler();
  1209. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading, heading);
  1210. };
  1211. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1212. }
  1213. void ScriptManager::Script_ArcBallCameraController_SetPitch(float pitch)
  1214. {
  1215. auto operation = [pitch]()
  1216. {
  1217. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1218. CheckArcBallControllerHandler();
  1219. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch, pitch);
  1220. };
  1221. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1222. }
  1223. void ScriptManager::Script_NoClipCameraController_SetPosition(AZ::Vector3 position)
  1224. {
  1225. auto operation = [position]()
  1226. {
  1227. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1228. CheckNoClipControllerHandler();
  1229. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPosition, position);
  1230. };
  1231. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1232. }
  1233. void ScriptManager::Script_NoClipCameraController_SetHeading(float heading)
  1234. {
  1235. auto operation = [heading]()
  1236. {
  1237. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1238. CheckNoClipControllerHandler();
  1239. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading, heading);
  1240. };
  1241. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1242. }
  1243. void ScriptManager::Script_NoClipCameraController_SetPitch(float pitch)
  1244. {
  1245. auto operation = [pitch]()
  1246. {
  1247. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1248. CheckNoClipControllerHandler();
  1249. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch, pitch);
  1250. };
  1251. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1252. }
  1253. void ScriptManager::Script_NoClipCameraController_SetFov(float fov)
  1254. {
  1255. auto operation = [fov]()
  1256. {
  1257. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1258. CheckNoClipControllerHandler();
  1259. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetFov, fov);
  1260. };
  1261. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1262. }
  1263. void ScriptManager::Script_AssetTracking_Start()
  1264. {
  1265. auto operation = []()
  1266. {
  1267. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1268. s_instance->m_assetStatusTracker.StartTracking();
  1269. };
  1270. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1271. }
  1272. void ScriptManager::Script_AssetTracking_ExpectAsset(const AZStd::string& sourceAssetPath, uint32_t expectedCount)
  1273. {
  1274. auto operation = [sourceAssetPath, expectedCount]()
  1275. {
  1276. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1277. s_instance->m_assetStatusTracker.ExpectAsset(sourceAssetPath, expectedCount);
  1278. };
  1279. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1280. }
  1281. void ScriptManager::Script_AssetTracking_IdleUntilExpectedAssetsFinish(float timeout)
  1282. {
  1283. auto operation = [timeout]()
  1284. {
  1285. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1286. AZ_Assert(!s_instance->m_waitForAssetTracker, "It shouldn't be possible to run the next command until m_waitForAssetTracker is false");
  1287. s_instance->m_waitForAssetTracker = true;
  1288. s_instance->m_assetTrackingTimeout = timeout;
  1289. };
  1290. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1291. }
  1292. void ScriptManager::Script_AssetTracking_Stop()
  1293. {
  1294. auto operation = []()
  1295. {
  1296. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1297. s_instance->m_assetStatusTracker.StopTracking();
  1298. };
  1299. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1300. }
  1301. } // namespace AtomSampleViewer