ScriptManager.cpp 77 KB

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