12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183 |
- /*
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <Automation/ScriptManager.h>
- #include <Automation/ScriptableImGui.h>
- #include <SampleComponentManagerBus.h>
- #include <imgui/imgui_internal.h>
- #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
- #include <Atom/RPI.Public/RPISystemInterface.h>
- #include <Atom/Component/DebugCamera/CameraComponent.h>
- #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
- #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
- #include <Atom/Feature/ImGui/SystemBus.h>
- #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
- #include <Atom/RHI/Factory.h>
- #include <AzCore/Component/Entity.h>
- #include <AzCore/Settings/SettingsRegistryScriptUtils.h>
- #include <AzCore/Script/ScriptContext.h>
- #include <AzCore/Script/ScriptSystemBus.h>
- #include <AzCore/Script/ScriptAsset.h>
- #include <AzCore/Math/MathReflection.h>
- #include <AzCore/Console/IConsole.h>
- #include <AzCore/Time/ITime.h>
- #include <AzFramework/API/ApplicationAPI.h>
- #include <AzFramework/Components/ConsoleBus.h>
- #include <AzFramework/IO/LocalFileIO.h>
- #include <AzFramework/Windowing/WindowBus.h>
- #include <AtomSampleViewerRequestBus.h>
- #include <Utils/Utils.h>
- namespace AtomSampleViewer
- {
- ScriptManager* ScriptManager::s_instance = nullptr;
- ScriptManager::ScriptManager()
- : m_scriptBrowser("@user@/lua_script_browser.xml")
- {
- }
- void ScriptManager::Activate()
- {
- AZ_Assert(s_instance == nullptr, "ScriptManager is already activated");
- s_instance = this;
- ScriptableImGui::Create();
- m_scriptContext = AZStd::make_unique<AZ::ScriptContext>();
- m_sriptBehaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
- ReflectScriptContext(m_sriptBehaviorContext.get());
- m_scriptContext->BindTo(m_sriptBehaviorContext.get());
- m_scriptBrowser.SetFilter([](const AZ::Data::AssetInfo& assetInfo)
- {
- return AzFramework::StringFunc::EndsWith(assetInfo.m_relativePath, ".bv.luac");
- });
- m_scriptBrowser.Activate();
- ScriptRepeaterRequestBus::Handler::BusConnect();
- ScriptRunnerRequestBus::Handler::BusConnect();
- m_imageComparisonOptions.Activate();
- }
- void ScriptManager::Deactivate()
- {
- s_instance = nullptr;
- m_scriptContext = nullptr;
- m_sriptBehaviorContext = nullptr;
- m_scriptBrowser.Deactivate();
- ScriptableImGui::Destory();
- m_imageComparisonOptions.Deactivate();
- ScriptRunnerRequestBus::Handler::BusDisconnect();
- ScriptRepeaterRequestBus::Handler::BusDisconnect();
- AZ::Debug::CameraControllerNotificationBus::Handler::BusDisconnect();
- }
- void ScriptManager::SetCameraEntity(AZ::Entity* cameraEntity)
- {
- AZ::Debug::CameraControllerNotificationBus::Handler::BusDisconnect();
- m_cameraEntity = cameraEntity;
- AZ::Debug::CameraControllerNotificationBus::Handler::BusConnect(m_cameraEntity->GetId());
- }
- void ScriptManager::PauseScript()
- {
- PauseScriptWithTimeout(DefaultPauseTimeout);
- }
- void ScriptManager::PauseScriptWithTimeout(float timeout)
- {
- m_scriptPaused = true;
- m_scriptPauseTimeout = AZ::GetMax(timeout, m_scriptPauseTimeout);
- }
- void ScriptManager::ResumeScript()
- {
- AZ_Warning("Automation", m_scriptPaused, "Script is not paused");
- m_scriptPaused = false;
- }
- void ScriptManager::ReportScriptError([[maybe_unused]] const AZStd::string& message)
- {
- AZ_Error("Automation", false, "Script: %s", message.c_str());
- }
- void ScriptManager::ReportScriptWarning([[maybe_unused]] const AZStd::string& message)
- {
- AZ_Warning("Automation", false, "Script: %s", message.c_str());
- }
- void ScriptManager::ReportScriptableAction([[maybe_unused]] AZStd::string_view scriptCommand)
- {
- AZ_TracePrintf("Automation", "Scriptable Action: %.*s\n", AZ_STRING_ARG(scriptCommand));
- }
- void ScriptManager::ImageComparisonOptions::Activate()
- {
- m_configAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::AnyAsset>("config/ImageComparisonConfig.azasset", AZ::RPI::AssetUtils::TraceLevel::Assert);
- if (m_configAsset)
- {
- AZ::Data::AssetBus::Handler::BusConnect(m_configAsset.GetId());
- OnAssetReloaded(m_configAsset);
- }
- }
- void ScriptManager::ImageComparisonOptions::Deactivate()
- {
- m_configAsset.Release();
- ResetImGuiSettings();
- AZ::Data::AssetBus::Handler::BusDisconnect();
- }
- ImageComparisonToleranceLevel* ScriptManager::ImageComparisonOptions::FindToleranceLevel(const AZStd::string& name, bool allowLevelAdjustment)
- {
- size_t foundIndex = m_config.m_toleranceLevels.size();
- for (size_t i = 0; i < m_config.m_toleranceLevels.size(); ++i)
- {
- if (m_config.m_toleranceLevels[i].m_name == name)
- {
- foundIndex = i;
- break;
- }
- }
- if (foundIndex == m_config.m_toleranceLevels.size())
- {
- return nullptr;
- }
- if (allowLevelAdjustment)
- {
- int adjustedIndex = aznumeric_cast<int>(foundIndex);
- adjustedIndex += m_toleranceAdjustment;
- adjustedIndex = AZ::GetClamp(adjustedIndex, 0, aznumeric_cast<int>(m_config.m_toleranceLevels.size()) - 1);
- foundIndex = aznumeric_cast<size_t>(adjustedIndex);
- }
- return &m_config.m_toleranceLevels[foundIndex];
- }
- const AZStd::vector<ImageComparisonToleranceLevel>& ScriptManager::ImageComparisonOptions::GetAvailableToleranceLevels() const
- {
- return m_config.m_toleranceLevels;
- }
- void ScriptManager::ImageComparisonOptions::SelectToleranceLevel(const AZStd::string& name, bool allowLevelAdjustment)
- {
- if (m_selectedOverrideSetting == OverrideSetting_ScriptControlled)
- {
- ImageComparisonToleranceLevel* level = FindToleranceLevel(name, allowLevelAdjustment);
- if (level)
- {
- m_currentToleranceLevel = level;
- }
- else
- {
- ReportScriptError(AZStd::string::format("ImageComparisonToleranceLevel '%s' not found.", name.c_str()));
- }
- }
- }
- void ScriptManager::ImageComparisonOptions::SelectToleranceLevel(ImageComparisonToleranceLevel* level)
- {
- if (nullptr == level)
- {
- m_currentToleranceLevel = level;
- return;
- }
- else
- {
- SelectToleranceLevel(level->m_name);
- AZ_Assert(GetCurrentToleranceLevel() == level, "Wrong ImageComparisonToleranceLevel pointer used");
- }
- }
- ImageComparisonToleranceLevel* ScriptManager::ImageComparisonOptions::GetCurrentToleranceLevel()
- {
- return m_currentToleranceLevel;
- }
- bool ScriptManager::ImageComparisonOptions::IsScriptControlled() const
- {
- return m_selectedOverrideSetting == OverrideSetting_ScriptControlled;
- }
- bool ScriptManager::ImageComparisonOptions::IsLevelAdjusted() const
- {
- return m_toleranceAdjustment != 0;
- }
- void ScriptManager::ImageComparisonOptions::DrawImGuiSettings()
- {
- ImGui::Text("Tolerance");
- ImGui::Indent();
- if (ImGui::Combo("Level",
- &m_selectedOverrideSetting,
- m_overrideSettings.data(),
- aznumeric_cast<int>(m_overrideSettings.size())))
- {
- if (m_selectedOverrideSetting == OverrideSetting_ScriptControlled)
- {
- m_currentToleranceLevel = nullptr;
- }
- else
- {
- m_currentToleranceLevel = &m_config.m_toleranceLevels[m_selectedOverrideSetting - 1];
- }
- }
- if (IsScriptControlled())
- {
- ImGui::InputInt("Level Adjustment", &m_toleranceAdjustment);
- }
- ImGui::Unindent();
- }
- void ScriptManager::ImageComparisonOptions::ResetImGuiSettings()
- {
- m_currentToleranceLevel = nullptr;
- m_selectedOverrideSetting = OverrideSetting_ScriptControlled;
- m_toleranceAdjustment = 0;
- }
- void ScriptManager::ImageComparisonOptions::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
- {
- m_configAsset = asset;
- m_config = *m_configAsset->GetDataAs<ImageComparisonConfig>();
- m_overrideSettings.clear();
- m_overrideSettings.push_back("[Script-controlled]");
- for (size_t i = 0; i < m_config.m_toleranceLevels.size(); ++i)
- {
- AZ_Assert(i == 0 || m_config.m_toleranceLevels[i].m_threshold > m_config.m_toleranceLevels[i - 1].m_threshold, "Threshold values are not sequential");
- m_overrideSettings.push_back(m_config.m_toleranceLevels[i].m_name.c_str());
- }
- }
- void ScriptManager::TickImGui()
- {
- if (m_showScriptRunnerDialog)
- {
- ShowScriptRunnerDialog();
- }
- m_scriptReporter.TickImGui();
- if (m_showPrecommitWizard)
- {
- ShowPrecommitWizard();
- }
- if (m_testSuiteRunConfig.m_automatedRunEnabled)
- {
- if (m_testSuiteRunConfig.m_isStarted == false)
- {
- m_testSuiteRunConfig.m_isStarted = true;
- PrepareAndExecuteScript(m_testSuiteRunConfig.m_testSuitePath);
- }
- }
- }
- void ScriptManager::TickScript(float deltaTime)
- {
- // All actions must be consumed each frame. Otherwise, this indicates that a script is
- // scheduling ScriptableImGui actions for fields that don't exist.
- ScriptableImGui::CheckAllActionsConsumed();
- ScriptableImGui::ClearActions();
- // We delayed PopScript() until after the above CheckAllActionsConsumed(), so that any errors
- // reported by that function will be associated with the proper script.
- if (m_shouldPopScript)
- {
- m_scriptReporter.PopScript();
- m_shouldPopScript = false;
- }
- while (!m_scriptOperations.empty())
- {
- if (m_shouldPopScript)
- {
- // If we just finished executing a script, the remaining m_scriptOperations are for some other script.
- // We need to proceed to the next frame and allow that PopScript() to happen, otherwise any errors related
- // to subsequent operations would be reported against the prior script.
- break;
- }
- if (m_scriptPaused)
- {
- m_scriptPauseTimeout -= deltaTime;
- if (m_scriptPauseTimeout < 0)
- {
- AZ_Error("Automation", false, "Script pause timed out. Continuing...");
- m_scriptPaused = false;
- }
- else
- {
- break;
- }
- }
- if (m_waitForAssetTracker)
- {
- m_assetTrackingTimeout -= deltaTime;
- if (m_assetTrackingTimeout < 0)
- {
- auto incomplateAssetList = m_assetStatusTracker.GetIncompleteAssetList();
- AZStd::string incompleteAssetListString;
- AzFramework::StringFunc::Join(incompleteAssetListString, incomplateAssetList.begin(), incomplateAssetList.end(), "\n ");
- AZ_Error("Automation", false, "Script asset tracking timed out waiting for:\n %s \n Continuing...", incompleteAssetListString.c_str());
- m_waitForAssetTracker = false;
- }
- else if (m_assetStatusTracker.DidExpectedAssetsFinish())
- {
- AZ_Printf("Automation", "Asset Tracker finished with %f seconds remaining.", m_assetTrackingTimeout);
- m_waitForAssetTracker = false;
- }
- else
- {
- break;
- }
- }
- if (m_scriptIdleFrames > 0)
- {
- m_scriptIdleFrames--;
- break;
- }
- if (m_scriptIdleSeconds > 0)
- {
- m_scriptIdleSeconds -= deltaTime;
- break;
- }
- // Execute the next operation
- m_scriptOperations.front()();
- m_scriptOperations.pop();
- if (m_scriptOperations.empty())
- {
- m_doFinalScriptCleanup = true;
- }
- }
- if (m_shouldPopScript)
- {
- // We need to proceed for one more frame to do the last PopScript() before final cleanup
- return;
- }
- if (m_doFinalScriptCleanup)
- {
- bool frameCapturePending = false;
- SampleComponentManagerRequestBus::BroadcastResult(frameCapturePending, &SampleComponentManagerRequests::IsFrameCapturePending);
- if (!frameCapturePending && !m_isCapturePending)
- {
- AZ_Assert(m_scriptPaused == false, "Script manager is in an unexpected state.");
- AZ_Assert(m_scriptIdleFrames == 0, "Script manager is in an unexpected state.");
- AZ_Assert(m_scriptIdleSeconds <= 0.0f, "Script manager is in an unexpected state.");
- AZ_Assert(m_waitForAssetTracker == false, "Script manager is in an unexpected state.");
- AZ_Assert(!m_scriptReporter.HasActiveScript(), "Script manager is in an unexpected state.");
- AZ_Assert(m_executingScripts.size() == 0, "Script manager is in an unexpected state");
- m_assetStatusTracker.StopTracking();
- if (m_frameTimeIsLocked)
- {
- AZ::Interface<AZ::IConsole>::Get()->PerformCommand("t_simulationTickDeltaOverride 0");
- m_frameTimeIsLocked = false;
- }
- if (m_shouldRestoreViewportSize)
- {
- Utils::ResizeClientArea(m_savedViewportWidth, m_savedViewportHeight, AzFramework::WindowPosOptions());
- m_shouldRestoreViewportSize = false;
- }
- // In case scripts were aborted while ImGui was temporarily hidden, show it again.
- SetShowImGui(true);
- m_scriptReporter.SortScriptReports();
- m_scriptReporter.OpenReportDialog();
- m_shouldPopScript = false;
- m_doFinalScriptCleanup = false;
- if (m_testSuiteRunConfig.m_automatedRunEnabled && m_testSuiteRunConfig.m_closeOnTestScriptFinish)
- {
- m_testSuiteRunConfig.m_automatedRunEnabled = false;
- if (m_scriptReporter.HasErrorsAssertsInReport())
- {
- AtomSampleViewerRequestsBus::Broadcast(&AtomSampleViewerRequestsBus::Events::SetExitCode, 1);
- // Useful console logging for Hydra tests
- int failedTests = 0;
- for (const ScriptReporter::ScriptReport& testReport : m_scriptReporter.GetScriptReport())
- {
- if (testReport.m_assertCount > 0 || testReport.m_generalErrorCount > 0 || testReport.m_screenshotErrorCount > 0)
- {
- ++failedTests;
- AZ_Printf("AtomSampleViewer", "Test failure %s: asserts %u, general errors %u, screenshot failures %u\n", testReport.m_scriptAssetPath.c_str(),
- testReport.m_assertCount, testReport.m_generalErrorCount, testReport.m_screenshotErrorCount);
- }
- }
- AZ_Printf("AtomSampleViewer", "%d tests failed\n", failedTests);
- }
- AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
- }
- }
- }
- }
- void ScriptManager::OpenScriptRunnerDialog()
- {
- m_showScriptRunnerDialog = true;
- }
- void ScriptManager::OpenPrecommitWizard()
- {
- m_showPrecommitWizard = true;
- }
- void ScriptManager::RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed)
- {
- m_testSuiteRunConfig.m_automatedRunEnabled = true;
- m_testSuiteRunConfig.m_testSuitePath = suiteFilePath;
- m_testSuiteRunConfig.m_closeOnTestScriptFinish = exitOnTestEnd;
- m_testSuiteRunConfig.m_randomSeed = randomSeed;
- }
- void ScriptManager::AbortScripts(const AZStd::string& reason)
- {
- m_scriptReporter.SetInvalidationMessage(reason);
- m_scriptOperations = {};
- m_executingScripts.clear();
- m_scriptPaused = false;
- m_scriptIdleFrames = 0;
- m_scriptIdleSeconds = 0.0f;
- m_waitForAssetTracker = false;
- while (m_scriptReporter.HasActiveScript())
- {
- m_scriptReporter.PopScript();
- }
- m_doFinalScriptCleanup = true;
- }
- void ScriptManager::ShowScriptRunnerDialog()
- {
- if (ImGui::Begin("Script Runner", &m_showScriptRunnerDialog))
- {
- auto drawAbortButton = [this](const char* uniqueId)
- {
- ImGui::PushID(uniqueId);
- if (ImGui::Button("Abort"))
- {
- AbortScripts("Script(s) manually aborted.");
- }
- ImGui::PopID();
- };
- // The main buttons are at the bottom, but show the Abort button at the top too, in case the window size is small.
- if (!m_scriptOperations.empty())
- {
- drawAbortButton("Button1");
- }
- ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
- assetBrowserSettings.m_labels.m_root = "Lua Scripts";
- m_scriptBrowser.Tick(assetBrowserSettings);
- AZStd::string selectedFileName = "<none>";
- AzFramework::StringFunc::Path::GetFullFileName(m_scriptBrowser.GetSelectedAssetPath().c_str(), selectedFileName);
- ImGui::LabelText("##SelectedScript", "Selected: %s", selectedFileName.c_str());
- ImGui::Separator();
- ImGui::Text("Settings");
- ImGui::Indent();
- ImGui::InputInt("Random Seed for Test Order Execution", &m_testSuiteRunConfig.m_randomSeed);
- m_imageComparisonOptions.DrawImGuiSettings();
- if (ImGui::Button("Reset"))
- {
- m_imageComparisonOptions.ResetImGuiSettings();
- }
- ImGui::Unindent();
- ImGui::Separator();
- if (ImGui::Button("Run"))
- {
- auto scriptAsset = m_scriptBrowser.GetSelectedAsset<AZ::ScriptAsset>();
- if (scriptAsset.GetId().IsValid())
- {
- PrepareAndExecuteScript(m_scriptBrowser.GetSelectedAssetPath());
- }
- }
- if (ImGui::Button("View Latest Results"))
- {
- m_scriptReporter.OpenReportDialog();
- }
- if (m_scriptOperations.size() > 0)
- {
- ImGui::LabelText("##RunningScript", "Running %zu operations...", m_scriptOperations.size());
- drawAbortButton("Button2");
- }
- }
- m_messageBox.TickPopup();
- ImGui::End();
- }
- void ScriptManager::ShowPrecommitWizard()
- {
- if (ImGui::Begin("Pre-commit Wizard", &m_showPrecommitWizard))
- {
- ShowBackToIntroWarning();
- if (ImGui::Button("Start from the Beginning"))
- {
- ImGui::OpenPopup("Return to Intro Window");
- }
- switch (m_wizardSettings.m_stage)
- {
- case PrecommitWizardSettings::Stage::Intro:
- ShowPrecommitWizardIntro();
- break;
- case PrecommitWizardSettings::Stage::RunFullsuiteTest:
- ShowPrecommitWizardRunFullsuiteTest();
- break;
- case PrecommitWizardSettings::Stage::ReportFullsuiteSummary:
- ShowPrecommitWizardReportFullsuiteTest();
- break;
- case PrecommitWizardSettings::Stage::ManualInspection:
- // Go through each pass in the full suite test and require users to manually pick from a list
- // to describe the difference if it exists.
- ShowPrecomitWizardManualInspection();
- break;
- case PrecommitWizardSettings::Stage::ReportFinalSummary:
- // Generate a summary that can be easily copied into the clipboard
- ShowPrecommitWizardReportFinalSummary();
- break;
- }
- }
- ImGui::End();
- }
- void ScriptManager::ShowBackToIntroWarning()
- {
- if (ImGui::BeginPopupModal("Return to Intro Window", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
- {
- ImGui::Text("Warning: You are returning to the intro window. Current full test suite results will be lost.\n\nAre you sure?");
- if (ImGui::Button("Yes"))
- {
- m_wizardSettings = PrecommitWizardSettings();
- ImGui::CloseCurrentPopup();
- }
- ImGui::SameLine();
- if (ImGui::Button("No"))
- {
- ImGui::CloseCurrentPopup();
- }
- ImGui::EndPopup();
- }
- }
- void ScriptManager::ShowPrecommitWizardIntro()
- {
- ImGui::Separator();
- ImGui::TextWrapped("Here's what will take place...\n"
- "1) The standard '_fulltestsuite_' script will be run.\n"
- "2) You'll see a summary of the test results. If any tests failed the automatic checks, "
- "you are strongly encouraged to address those issues first.\n"
- "3) You will be guided through a series of visual screenshot validations. For each one, "
- "you'll need to inspect an image comparison and indicate whether you see visual differences.\n"
- "4) The final report will be generated. Copy and paste this into your PR description.");
- ImGui::Separator();
- if (ImGui::Button("Run Full Test Suite"))
- {
- PrepareAndExecuteScript(FullSuiteScriptFilepath);
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::RunFullsuiteTest;
- }
- }
- void ScriptManager::ShowPrecommitWizardRunFullsuiteTest()
- {
- ImGui::Text("Running Full Test Suite. Please Wait...");
- if (m_scriptOperations.empty() && !m_doFinalScriptCleanup)
- {
- m_scriptReporter.HideReportDialog();
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFullsuiteSummary;
- }
- }
- void ScriptManager::ShowPrecommitWizardReportFullsuiteTest()
- {
- m_scriptReporter.DisplayScriptResultsSummary();
- if (ImGui::Button("See Details..."))
- {
- m_scriptReporter.OpenReportDialog();
- }
- ImGui::Separator();
- if (ImGui::Button("Back"))
- {
- ImGui::OpenPopup("Return to Intro Window");
- }
- ImGui::SameLine();
- if (ImGui::Button("Continue"))
- {
- m_wizardSettings.ProcessScriptReports(m_scriptReporter.GetScriptReport());
- if (!m_wizardSettings.m_reportsOrderedByThresholdToInspect.empty())
- {
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ManualInspection;
- }
- else
- {
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFinalSummary;
- }
- }
- }
- void ScriptManager::ShowPrecomitWizardManualInspection()
- {
- const AZStd::vector<ScriptReporter::ScriptReport>& scriptReports = m_scriptReporter.GetScriptReport();
- // Get the script report and screenshot test corresponding to the current index
- const ScriptReporter::ReportIndex currentIndex = m_wizardSettings.m_reportIterator->second;
- const ScriptReporter::ScriptReport& scriptReport = scriptReports[currentIndex.first];
- const ScriptReporter::ScreenshotTestInfo& screenshotTest = scriptReport.m_screenshotTests[currentIndex.second];
- ImGui::Text("Script Name: %s", scriptReport.m_scriptAssetPath.c_str());
- ImGui::Text("Screenshot Name: %s", screenshotTest.m_officialBaselineScreenshotFilePath.c_str());
- ImGui::Separator();
- ImGui::Text("Diff Score: %f", screenshotTest.m_officialComparisonResult.m_diffScore);
- // TODO: Render screenshots in ImGui.
- ImGui::Separator();
- // Check if the current index's screenshot has been manually validated and let the user know
- auto it = m_wizardSettings.m_reportIndexDifferenceLevelMap.find(currentIndex);
- if (it != m_wizardSettings.m_reportIndexDifferenceLevelMap.end())
- {
- m_wizardSettings.m_inspectionSelection = it->second;
- ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 255, 0, 255));
- ImGui::Text("This inspection has been added to the report summary");
- ImGui::PopStyleColor();
- }
- else
- {
- ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 0, 0, 255));
- ImGui::Text("This inspection has not been added to the report summary");
- ImGui::PopStyleColor();
- }
- ImGui::TextWrapped(
- "Use the 'Export Png' button to inspect the screenshot. Choose from the options below that describe the difference level "
- "then click 'Next' to add it in the report summary. Once there are no more noticeable differences as the threshold decreases, "
- "click 'Finish' to generate the report summary. Note that the remaining screenshots that were not manually inspected will not "
- "appear in the report summary.");
- for (int i = 0; i < 4; ++i)
- {
- ImGui::RadioButton(PrecommitWizardSettings::ManualInspectionOptions[i], &m_wizardSettings.m_inspectionSelection, i);
- }
- if (ImGui::Button("View Diff"))
- {
- if (!Utils::RunDiffTool(screenshotTest.m_officialBaselineScreenshotFilePath, screenshotTest.m_screenshotFilePath))
- {
- ImGui::OpenPopup("Cannot open diff tool");
- }
- }
- if (ImGui::BeginPopupModal("Cannot open diff tool", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
- {
- ImGui::Text("Image diff is not supported on this platform, or the required diff tool is not installed.");
- if (ImGui::Button("Ok"))
- {
- ImGui::CloseCurrentPopup();
- }
- ImGui::EndPopup();
- }
- ImGui::SameLine();
- if (ImGui::Button("Export Png"))
- {
- m_wizardSettings.m_exportedPngPath = m_scriptReporter.ExportImageDiff(scriptReport, screenshotTest);
- }
- if (!m_wizardSettings.m_exportedPngPath.empty())
- {
- ImGui::SameLine();
- bool copyPath = ImGui::Button("Copy Path");
- ImGui::Text("Diff Png was exported to:");
- ImGui::SameLine();
- if (copyPath)
- {
- ImGui::LogToClipboard();
- }
- ImGui::Text("%s", m_wizardSettings.m_exportedPngPath.c_str());
- if (copyPath)
- {
- ImGui::LogFinish();
- }
- }
- ImGui::Separator();
- if (ImGui::Button("Back"))
- {
- if (m_wizardSettings.m_reportIterator == m_wizardSettings.m_reportsOrderedByThresholdToInspect.begin())
- {
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFullsuiteSummary;
- }
- else
- {
- --m_wizardSettings.m_reportIterator;
- }
- }
- ImGui::SameLine();
- const bool isNextButtonDisabled =
- m_wizardSettings.m_inspectionSelection == PrecommitWizardSettings::DefaultInspectionSelection ? true : false;
- if (isNextButtonDisabled)
- {
- ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
- ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
- }
- if (ImGui::Button("Next"))
- {
- m_wizardSettings.m_reportIndexDifferenceLevelMap[currentIndex] =
- PrecommitWizardSettings::ImageDifferenceLevel::Levels(m_wizardSettings.m_inspectionSelection);
- m_wizardSettings.m_inspectionSelection = PrecommitWizardSettings::DefaultInspectionSelection;
- m_wizardSettings.m_exportedPngPath.clear();
- ++m_wizardSettings.m_reportIterator;
- if (m_wizardSettings.m_reportIterator == m_wizardSettings.m_reportsOrderedByThresholdToInspect.end())
- {
- // Decrement the iterator to make sure it's pointing to the last element in case the user goes
- // back to the manual inspection page.
- --m_wizardSettings.m_reportIterator;
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFinalSummary;
- }
- }
- if (isNextButtonDisabled)
- {
- ImGui::PopItemFlag();
- ImGui::PopStyleVar();
- }
- ImGui::SameLine();
- if (ImGui::Button("Finish Manual Inspection"))
- {
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ReportFinalSummary;
- }
- ImGui::SameLine();
- ImGui::Text(
- "%zu/%zu", aznumeric_cast<size_t>(AZStd::distance(m_wizardSettings.m_reportsOrderedByThresholdToInspect.begin(), m_wizardSettings.m_reportIterator) + 1),
- m_wizardSettings.m_reportsOrderedByThresholdToInspect.size());
- }
- void ScriptManager::ShowPrecommitWizardReportFinalSummary()
- {
- ImGui::Text("Tests are complete! here is the final report.");
- bool copyToClipboard = ImGui::Button("Copy To Clipboard");
- const ScriptReporter::ScriptResultsSummary resultsSummary = m_scriptReporter.GetScriptResultSummary();
- const AZStd::vector<ScriptReporter::ScriptReport>& scriptReports = m_scriptReporter.GetScriptReport();
- AZStd::vector<AZStd::vector<ScriptReporter::ReportIndex>> imageDifferenceSummary(
- PrecommitWizardSettings::ImageDifferenceLevel::NumDifferenceLevels);
- int totalValidatedScreenshots = 0;
- for (const auto& [reportIndex, differenceLevel] : m_wizardSettings.m_reportIndexDifferenceLevelMap)
- {
- imageDifferenceSummary[differenceLevel].push_back(reportIndex);
- }
- for (const auto& differenceLevelReports : imageDifferenceSummary)
- {
- totalValidatedScreenshots += aznumeric_cast<int>(differenceLevelReports.size());
- }
- ImGui::Separator();
- if (copyToClipboard)
- {
- ImGui::LogToClipboard();
- }
- ImGui::Text("RHI API: %s", AZ::RHI::Factory::Get().GetName().GetCStr());
- ImGui::Text("Test Script Count: %zu", scriptReports.size());
- ImGui::Text("Screenshot Count: %d", resultsSummary.m_totalScreenshotsCount);
- ImGui::Text(
- "Manually Validated Screenshots: %d/%zu", totalValidatedScreenshots,
- m_wizardSettings.m_reportsOrderedByThresholdToInspect.size());
- ImGui::Text("Screenshot automatic checks failed: %zu", m_wizardSettings.m_failedReports.size());
- for (const auto& failedReport : m_wizardSettings.m_failedReports)
- {
- const ScriptReporter::ReportIndex& reportIndex = failedReport.second;
- const ScriptReporter::ScriptReport& scriptReport = scriptReports[reportIndex.first];
- const ScriptReporter::ScreenshotTestInfo& screenshotTest = scriptReport.m_screenshotTests[reportIndex.second];
- ImGui::Text(
- "\t%s %s '%s' %f", scriptReport.m_scriptAssetPath.c_str(), screenshotTest.m_screenshotFilePath.c_str(),
- screenshotTest.m_toleranceLevel.m_name.c_str(), screenshotTest.m_officialComparisonResult.m_diffScore);
- }
- // Present the information by printing highest differences first. See enum class ImageDifferenceLevel
- for (int i = aznumeric_cast<int>(imageDifferenceSummary.size() - 1); i >= 0; --i)
- {
- if (!imageDifferenceSummary[i].empty())
- {
- ImGui::Text("Screenshot interactive check '%s'", PrecommitWizardSettings::ManualInspectionDifferenceLevels[i]);
- for (const auto& reportIndex : imageDifferenceSummary[i])
- {
- const ScriptReporter::ScriptReport& scriptReport = scriptReports[reportIndex.first];
- const ScriptReporter::ScreenshotTestInfo& screenshotTest = scriptReport.m_screenshotTests[reportIndex.second];
- ImGui::Text(
- "\t%s %s '%s' %f", scriptReport.m_scriptAssetPath.c_str(), screenshotTest.m_screenshotFilePath.c_str(),
- screenshotTest.m_toleranceLevel.m_name.c_str(), screenshotTest.m_officialComparisonResult.m_diffScore);
- }
- }
- }
- if (copyToClipboard)
- {
- ImGui::LogFinish();
- }
- ImGui::Separator();
- if (ImGui::Button("Back"))
- {
- m_wizardSettings.m_stage = PrecommitWizardSettings::Stage::ManualInspection;
- auto reportIndexToDifference = m_wizardSettings.m_reportIndexDifferenceLevelMap.find(m_wizardSettings.m_reportIterator->second);
- m_wizardSettings.m_inspectionSelection = reportIndexToDifference != m_wizardSettings.m_reportIndexDifferenceLevelMap.end()
- ? reportIndexToDifference->second
- : PrecommitWizardSettings::DefaultInspectionSelection;
- }
- }
- void ScriptManager::PrepareAndExecuteScript(const AZStd::string& scriptFilePath)
- {
- ReportScriptableAction(AZStd::string::format("RunScript('%s')", scriptFilePath.c_str()));
- // Save the window size so we can restore it after running the script, in case the script calls ResizeViewport
- AzFramework::NativeWindowHandle defaultWindowHandle;
- AzFramework::WindowSize windowSize;
- AzFramework::WindowSystemRequestBus::BroadcastResult(defaultWindowHandle, &AzFramework::WindowSystemRequestBus::Events::GetDefaultWindowHandle);
- AzFramework::WindowRequestBus::EventResult(windowSize, defaultWindowHandle, &AzFramework::WindowRequests::GetClientAreaSize);
- m_savedViewportWidth = windowSize.m_width;
- m_savedViewportHeight = windowSize.m_height;
- if (m_savedViewportWidth == 0 || m_savedViewportHeight == 0)
- {
- AZ_Assert(false, "Could not get current window size");
- }
- else
- {
- m_shouldRestoreViewportSize = true;
- }
- // Setup the ScriptReporter to track and report the results
- m_scriptReporter.Reset();
- m_scriptReporter.SetAvailableToleranceLevels(m_imageComparisonOptions.GetAvailableToleranceLevels());
- if (m_imageComparisonOptions.IsLevelAdjusted())
- {
- m_scriptReporter.SetInvalidationMessage("Results are invalid because the tolerance level has been adjusted.");
- }
- else if (!m_imageComparisonOptions.IsScriptControlled())
- {
- m_scriptReporter.SetInvalidationMessage("Results are invalid because the tolerance level has been overridden.");
- }
- else
- {
- m_scriptReporter.SetInvalidationMessage("");
- }
- AZ_Assert(m_executingScripts.empty(), "There should be no active scripts at this point");
- ExecuteScript(scriptFilePath);
- }
- void ScriptManager::ExecuteScript(const AZStd::string& scriptFilePath)
- {
- AZ::Data::Asset<AZ::ScriptAsset> scriptAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::ScriptAsset>(scriptFilePath.c_str());
- if (!scriptAsset)
- {
- // Push an error operation on the back of the queue instead of reporting it immediately so it doesn't get lost
- // in front of a bunch of queued m_scriptOperations.
- Script_Error(AZStd::string::format("Could not find or load script asset '%s'.", scriptFilePath.c_str()));
- return;
- }
- if (s_instance->m_executingScripts.find(scriptAsset.GetId()) != s_instance->m_executingScripts.end())
- {
- Script_Error(AZStd::string::format("Calling script '%s' would likely cause an infinite loop and crash. Skipping.", scriptFilePath.c_str()));
- return;
- }
- if (s_instance->m_imageComparisonOptions.IsScriptControlled())
- {
- s_instance->m_imageComparisonOptions.SelectToleranceLevel(nullptr); // Clear the preset before each script to make sure the script is selecting it.
- }
- // Execute(script) will add commands to the m_scriptOperations. These should be considered part of their own test script, for reporting purposes.
- s_instance->m_scriptOperations.push([scriptFilePath]()
- {
- s_instance->m_scriptReporter.PushScript(scriptFilePath);
- }
- );
- s_instance->m_scriptOperations.push([scriptFilePath]()
- {
- AZ_Printf("Automation", "Running script '%s'...\n", scriptFilePath.c_str());
- }
- );
- s_instance->m_executingScripts.insert(scriptAsset.GetId());
- auto& scriptData = scriptAsset->m_data;
- if (!s_instance->m_scriptContext->Execute(scriptData.GetScriptBuffer().data(), scriptFilePath.c_str(), scriptData.GetScriptBuffer().size()))
- {
- // Push an error operation on the back of the queue instead of reporting it immediately so it doesn't get lost
- // in front of a bunch of queued m_scriptOperations.
- Script_Error(AZStd::string::format("Error running script '%s'.", scriptAsset.ToString<AZStd::string>().c_str()));
- }
- s_instance->m_executingScripts.erase(scriptAsset.GetId());
- // Execute(script) will have added commands to the m_scriptOperations. When they finish, consider this test as completed, for reporting purposes.
- s_instance->m_scriptOperations.push([]()
- {
- // We don't call m_scriptReporter.PopScript() yet because some cleanup needs to happen in TickScript() on the next frame.
- AZ_Assert(!s_instance->m_shouldPopScript, "m_shouldPopScript is already true");
- s_instance->m_shouldPopScript = true;
- }
- );
- }
- void ScriptManager::OnCameraMoveEnded(AZ::TypeId controllerTypeId, uint32_t channels)
- {
- if (controllerTypeId == azrtti_typeid<AZ::Debug::ArcBallControllerComponent>())
- {
- if (channels & AZ::Debug::ArcBallControllerChannel_Center)
- {
- AZ::Vector3 center = AZ::Vector3::CreateZero();
- AZ::Debug::ArcBallControllerRequestBus::EventResult(center, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetCenter);
- ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetCenter(Vector3(%f, %f, %f))", (float)center.GetX(), (float)center.GetY(), (float)center.GetZ()));
- }
- if (channels & AZ::Debug::ArcBallControllerChannel_Pan)
- {
- AZ::Vector3 pan = AZ::Vector3::CreateZero();
- AZ::Debug::ArcBallControllerRequestBus::EventResult(pan, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetPan);
- ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetPan(Vector3(%f, %f, %f))", (float)pan.GetX(), (float)pan.GetY(), (float)pan.GetZ()));
- }
- if (channels & AZ::Debug::ArcBallControllerChannel_Heading)
- {
- float heading = 0.0;
- AZ::Debug::ArcBallControllerRequestBus::EventResult(heading, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetHeading);
- ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetHeading(DegToRad(%f))", AZ::RadToDeg(heading)));
- }
- if (channels & AZ::Debug::ArcBallControllerChannel_Pitch)
- {
- float pitch = 0.0;
- AZ::Debug::ArcBallControllerRequestBus::EventResult(pitch, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetPitch);
- ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetPitch(DegToRad(%f))", AZ::RadToDeg(pitch)));
- }
- if (channels & AZ::Debug::ArcBallControllerChannel_Distance)
- {
- float distance = 0.0;
- AZ::Debug::ArcBallControllerRequestBus::EventResult(distance, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetDistance);
- ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetDistance(%f)", distance));
- }
- }
- if (controllerTypeId == azrtti_typeid<AZ::Debug::NoClipControllerComponent>())
- {
- if (channels & AZ::Debug::NoClipControllerChannel_Position)
- {
- AZ::Vector3 position = AZ::Vector3::CreateZero();
- AZ::Debug::NoClipControllerRequestBus::EventResult(position, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetPosition);
- ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetPosition(Vector3(%f, %f, %f))", (float)position.GetX(), (float)position.GetY(), (float)position.GetZ()));
- }
- if (channels & AZ::Debug::NoClipControllerChannel_Orientation)
- {
- float heading = 0.0;
- AZ::Debug::NoClipControllerRequestBus::EventResult(heading, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetHeading);
- ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetHeading(DegToRad(%f))", AZ::RadToDeg(heading)));
- float pitch = 0.0;
- AZ::Debug::NoClipControllerRequestBus::EventResult(pitch, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetPitch);
- ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetPitch(DegToRad(%f))", AZ::RadToDeg(pitch)));
- }
- if (channels & AZ::Debug::NoClipControllerChannel_Fov)
- {
- float fov = 0.0;
- AZ::Debug::NoClipControllerRequestBus::EventResult(fov, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetFov);
- ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetFov(DegToRad(%f))", AZ::RadToDeg(fov)));
- }
- }
- }
- void ScriptManager::ReflectScriptContext(AZ::BehaviorContext* behaviorContext)
- {
- AZ::MathReflect(behaviorContext);
- AZ::SettingsRegistryScriptUtils::ReflectSettingsRegistryToBehaviorContext(*behaviorContext);
- // Utilities...
- behaviorContext->Method("RunScript", &Script_RunScript);
- behaviorContext->Method("Error", &Script_Error);
- behaviorContext->Method("Warning", &Script_Warning);
- behaviorContext->Method("Print", &Script_Print);
- behaviorContext->Method("IdleFrames", &Script_IdleFrames);
- behaviorContext->Method("IdleSeconds", &Script_IdleSeconds);
- behaviorContext->Method("LockFrameTime", &Script_LockFrameTime);
- behaviorContext->Method("UnlockFrameTime", &Script_UnlockFrameTime);
- behaviorContext->Method("ResizeViewport", &Script_ResizeViewport);
- behaviorContext->Method("SetShowImGui", &Script_SetShowImGui);
- behaviorContext->Method("ExecuteConsoleCommand", &Script_ExecuteConsoleCommand);
- // Utilities returning data (these special functions do return data because they don't read dynamic state)...
- behaviorContext->Method("ResolvePath", &Script_ResolvePath);
- behaviorContext->Method("NormalizePath", &Script_NormalizePath);
- behaviorContext->Method("DegToRad", &Script_DegToRad);
- behaviorContext->Method("GetRenderApiName", &Script_GetRenderApiName);
- behaviorContext->Method("GetRandomTestSeed", &Script_GetRandomTestSeed);
- // Samples...
- behaviorContext->Method("OpenSample", &Script_OpenSample);
- behaviorContext->Method("SetImguiValue", &Script_SetImguiValue);
- // Debug profilers...
- behaviorContext->Method("ShowTool", &Script_ShowTool);
- // Screenshots...
- behaviorContext->Method("SetScreenshotFolder", &Script_SetScreenshotFolder);
- behaviorContext->Method("SetTestEnvPath", &Script_SetTestEnvPath);
- behaviorContext->Method("SetOfficialBaselineImageFolder", &Script_SetOfficialBaselineImageFolder);
- behaviorContext->Method("SetLocalBaselineImageFolder", &Script_SetLocalBaselineImageFolder);
- behaviorContext->Method("SelectImageComparisonToleranceLevel", &Script_SelectImageComparisonToleranceLevel);
- behaviorContext->Method("CaptureScreenshot", &Script_CaptureScreenshot);
- behaviorContext->Method("CaptureScreenshotWithImGui", &Script_CaptureScreenshotWithImGui);
- behaviorContext->Method("CaptureScreenshotWithPreview", &Script_CaptureScreenshotWithPreview);
- behaviorContext->Method("CapturePassAttachment", &Script_CapturePassAttachment);
- // Profiling data...
- behaviorContext->Method("CapturePassTimestamp", &Script_CapturePassTimestamp);
- behaviorContext->Method("CaptureCpuFrameTime", &Script_CaptureCpuFrameTime);
- behaviorContext->Method("CapturePassPipelineStatistics", &Script_CapturePassPipelineStatistics);
- behaviorContext->Method("CaptureCpuProfilingStatistics", &Script_CaptureCpuProfilingStatistics);
- behaviorContext->Method("CaptureBenchmarkMetadata", &Script_CaptureBenchmarkMetadata);
- // Camera...
- behaviorContext->Method("ArcBallCameraController_SetCenter", &Script_ArcBallCameraController_SetCenter);
- behaviorContext->Method("ArcBallCameraController_SetPan", &Script_ArcBallCameraController_SetPan);
- behaviorContext->Method("ArcBallCameraController_SetDistance", &Script_ArcBallCameraController_SetDistance);
- behaviorContext->Method("ArcBallCameraController_SetHeading", &Script_ArcBallCameraController_SetHeading);
- behaviorContext->Method("ArcBallCameraController_SetPitch", &Script_ArcBallCameraController_SetPitch);
- behaviorContext->Method("NoClipCameraController_SetPosition", &Script_NoClipCameraController_SetPosition);
- behaviorContext->Method("NoClipCameraController_SetHeading", &Script_NoClipCameraController_SetHeading);
- behaviorContext->Method("NoClipCameraController_SetPitch", &Script_NoClipCameraController_SetPitch);
- behaviorContext->Method("NoClipCameraController_SetFov", &Script_NoClipCameraController_SetFov);
- // Asset System...
- AZ::BehaviorParameterOverrides expectedCountDetails = {"expectedCount", "Expected number of asset jobs; default=1", aznew AZ::BehaviorDefaultValue(1u)};
- const AZStd::array<AZ::BehaviorParameterOverrides, 2> assetTrackingExpectAssetArgs = {{ AZ::BehaviorParameterOverrides{}, expectedCountDetails }};
- behaviorContext->Method("AssetTracking_Start", &Script_AssetTracking_Start);
- behaviorContext->Method("AssetTracking_ExpectAsset", &Script_AssetTracking_ExpectAsset, assetTrackingExpectAssetArgs);
- behaviorContext->Method("AssetTracking_IdleUntilExpectedAssetsFinish", &Script_AssetTracking_IdleUntilExpectedAssetsFinish);
- behaviorContext->Method("AssetTracking_Stop", &Script_AssetTracking_Stop);
- }
- void ScriptManager::Script_Error(const AZStd::string& message)
- {
- auto func = [message]()
- {
- ReportScriptError(message.c_str());
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- void ScriptManager::Script_Warning(const AZStd::string& message)
- {
- auto func = [message]()
- {
- ReportScriptWarning(message.c_str());
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- void ScriptManager::Script_Print(const AZStd::string& message [[maybe_unused]])
- {
- #ifndef RELEASE // AZ_TracePrintf is a no-op in release builds
- auto func = [message]()
- {
- AZ_TracePrintf("Automation", "Script: %s\n", message.c_str());
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- #endif
- }
- AZStd::string ScriptManager::Script_ResolvePath(const AZStd::string& path)
- {
- return Utils::ResolvePath(path);
- }
- AZStd::string ScriptManager::Script_NormalizePath(const AZStd::string& path)
- {
- AZStd::string normalizedPath = path;
- AzFramework::StringFunc::Path::Normalize(normalizedPath);
- return normalizedPath;
- }
- void ScriptManager::Script_OpenSample(const AZStd::string& sampleName)
- {
- auto operation = [sampleName]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- if (sampleName.empty())
- {
- SampleComponentManagerRequestBus::Broadcast(&SampleComponentManagerRequests::Reset);
- }
- else
- {
- bool foundSample = false;
- SampleComponentManagerRequestBus::BroadcastResult(foundSample, &SampleComponentManagerRequests::OpenSample, sampleName);
- if (foundSample)
- {
- // Samples need a few frames to initialize before consuming actions from ScriptableImGui,
- // so we need to wait before letting the script schedule ScriptableImGui actions.
- // They need 1 frame to activate, 1 frame to start ticking, and 1 frame to guarantee
- // that a sample OnTick occurs before a ScriptManager::OnTick. We schedule
- // a few extra just in case.
- AZ_Assert(s_instance->m_scriptIdleFrames == 0, "m_scriptIdleFrames is being stomped");
- s_instance->m_scriptIdleFrames = 6;
- }
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_ShowTool(const AZStd::string& toolName, bool enable)
- {
- auto operation = [toolName, enable]()
- {
- bool foundTool = false;
- SampleComponentManagerRequestBus::BroadcastResult(foundTool, &SampleComponentManagerRequests::ShowTool, toolName, enable);
- AZ_Warning("ScriptManager", foundTool, "Can't find [%s] tool", toolName.c_str());
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_RunScript(const AZStd::string& scriptFilePath)
- {
- // Unlike other Script_ callback functions, we process immediately instead of pushing onto the m_scriptOperations queue.
- // This function is special because running the script is what adds more commands onto the m_scriptOperations queue.
- s_instance->ExecuteScript(scriptFilePath);
- }
- void ScriptManager::Script_IdleFrames(int numFrames)
- {
- auto operation = [numFrames]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- AZ_Assert(s_instance->m_scriptIdleFrames == 0, "m_scriptIdleFrames is being stomped");
- s_instance->m_scriptIdleFrames = numFrames;
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_IdleSeconds(float numSeconds)
- {
- auto operation = [numSeconds]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- s_instance->m_scriptIdleSeconds = numSeconds;
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_LockFrameTime(float seconds)
- {
- auto operation = [seconds]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- AZ::TimeUs us = AZ::SecondsToTimeUs(seconds);
-
- AZ::CVarFixedString commandString = AZ::CVarFixedString::format("t_simulationTickDeltaOverride %" PRId64, static_cast<int64_t>(us));
- AZ::Interface<AZ::IConsole>::Get()->PerformCommand(commandString.c_str());
- s_instance->m_frameTimeIsLocked = true;
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_UnlockFrameTime()
- {
- auto operation = []()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- AZ::Interface<AZ::IConsole>::Get()->PerformCommand("t_simulationTickDeltaOverride 0");
- s_instance->m_frameTimeIsLocked = false;
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_SetImguiValue(AZ::ScriptDataContext& dc)
- {
- if (dc.GetNumArguments() != 2)
- {
- ReportScriptError("Wrong number of arguments for SetImguiValue");
- return;
- }
- if (!dc.IsString(0))
- {
- ReportScriptError("SetImguiValue first argument must be a string");
- return;
- }
- const char* fieldName = nullptr;
- dc.ReadArg(0, fieldName);
- AZStd::string fieldNameString = fieldName; // Because the lambda will need to capture a copy of something, not a pointer
- if (dc.IsBoolean(1))
- {
- bool value = false;
- dc.ReadArg(1, value);
- auto func = [fieldNameString, value]()
- {
- ScriptableImGui::SetBool(fieldNameString, value);
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- else if (dc.IsNumber(1))
- {
- float value = 0.0f;
- dc.ReadArg(1, value);
- auto func = [fieldNameString, value]()
- {
- ScriptableImGui::SetNumber(fieldNameString, value);
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- else if (dc.IsString(1))
- {
- const char* value = nullptr;
- dc.ReadArg(1, value);
- AZStd::string valueString = value; // Because the lambda will need to capture a copy of something, not a pointer
- auto func = [fieldNameString, valueString]()
- {
- ScriptableImGui::SetString(fieldNameString, valueString);
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- else if (dc.IsClass<AZ::Vector3>(1))
- {
- AZ::Vector3 value = AZ::Vector3::CreateZero();
- dc.ReadArg(1, value);
- auto func = [fieldNameString, value]()
- {
- ScriptableImGui::SetVector(fieldNameString, value);
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- else if (dc.IsClass<AZ::Vector2>(1))
- {
- AZ::Vector2 value = AZ::Vector2::CreateZero();
- dc.ReadArg(1, value);
- auto func = [fieldNameString, value]()
- {
- ScriptableImGui::SetVector(fieldNameString, value);
- };
- s_instance->m_scriptOperations.push(AZStd::move(func));
- }
- }
- void ScriptManager::Script_ResizeViewport(int width, int height)
- {
- auto operation = [width,height]()
- {
- if (Utils::SupportsResizeClientArea())
- {
- AzFramework::WindowPosOptions options;
- options.m_ignoreScreenSizeLimit = true;
- Utils::ResizeClientArea(width, height, options);
- }
- else
- {
- s_instance->ReportScriptError("ResizeViewport() is not supported on this platform");
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_ExecuteConsoleCommand(const AZStd::string& command)
- {
- auto operation = [command]()
- {
- AZ::Interface<AZ::IConsole>::Get()->PerformCommand(command.c_str());
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::SetShowImGui(bool show)
- {
- m_prevShowImGui = m_showImGui;
- if (show)
- {
- AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::ShowAllImGuiPasses);
- }
- else
- {
- AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::HideAllImGuiPasses);
- }
- m_showImGui = show;
- }
- void ScriptManager::Script_SetShowImGui(bool show)
- {
- auto operation = [show]()
- {
- s_instance->SetShowImGui(show);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- bool ScriptManager::PrepareForScreenCapture(const AZStd::string& imageName)
- {
- AZ::Render::FrameCapturePathOutcome pathOutcome;
- AZ::Render::FrameCaptureTestRequestBus::BroadcastResult(
- pathOutcome,
- &AZ::Render::FrameCaptureTestRequestBus::Events::BuildScreenshotFilePath,
- imageName, true);
- if (!pathOutcome.IsSuccess())
- {
- ReportScriptError(pathOutcome.GetError().m_errorMessage);
- return false;
- }
- AZStd::string fullFilePath = pathOutcome.IsSuccess() ? pathOutcome.GetValue() : "";
- // 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.
- if (AZ::IO::LocalFileIO::GetInstance()->Exists(fullFilePath.c_str()) && !AZ::IO::LocalFileIO::GetInstance()->Remove(fullFilePath.c_str()))
- {
- ReportScriptError(AZStd::string::format("Failed to delete existing screenshot file '%s'.", fullFilePath.c_str()));
- return false;
- }
- s_instance->m_scriptReporter.AddScreenshotTest(imageName);
- s_instance->m_isCapturePending = true;
- s_instance->PauseScript();
- return true;
- }
- void ScriptManager::Script_SetScreenshotFolder(const AZStd::string& screenshotFolder)
- {
- auto operation = [screenshotFolder]()
- {
- AZ::Render::FrameCaptureTestRequestBus::Broadcast(
- &AZ::Render::FrameCaptureTestRequestBus::Events::SetScreenshotFolder, screenshotFolder);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_SetTestEnvPath(const AZStd::string& envPath)
- {
- auto operation = [envPath]()
- {
- AZ::Render::FrameCaptureTestRequestBus::Broadcast(
- &AZ::Render::FrameCaptureTestRequestBus::Events::SetTestEnvPath, envPath);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_SetOfficialBaselineImageFolder(const AZStd::string& baselineFolder)
- {
- auto operation = [baselineFolder]()
- {
- AZ::Render::FrameCaptureTestRequestBus::Broadcast(
- &AZ::Render::FrameCaptureTestRequestBus::Events::SetOfficialBaselineImageFolder, baselineFolder);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_SetLocalBaselineImageFolder(const AZStd::string& baselineFolder)
- {
- auto operation = [baselineFolder]()
- {
- AZ::Render::FrameCaptureTestRequestBus::Broadcast(
- &AZ::Render::FrameCaptureTestRequestBus::Events::SetLocalBaselineImageFolder, baselineFolder);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_SelectImageComparisonToleranceLevel(const AZStd::string& presetName)
- {
- auto operation = [presetName]()
- {
- s_instance->m_imageComparisonOptions.SelectToleranceLevel(presetName);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_CaptureScreenshot(const AZStd::string& imageName)
- {
- Script_SetShowImGui(false);
- auto operation = [imageName]()
- {
- // Note this will pause the script until the capture is complete
- if (PrepareForScreenCapture(imageName))
- {
- AZ::Render::FrameCapturePathOutcome pathOutcome;
- AZ::Render::FrameCaptureTestRequestBus::BroadcastResult(
- pathOutcome,
- &AZ::Render::FrameCaptureTestRequestBus::Events::BuildScreenshotFilePath,
- imageName, true);
- if (!pathOutcome.IsSuccess())
- {
- ReportScriptError(pathOutcome.GetError().m_errorMessage);
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- AZStd::string screenshotFilePath = pathOutcome.GetValue();
- AZ_Assert(s_instance->m_frameCaptureId == AZ::Render::InvalidFrameCaptureId, "Attempting to start a capture while one is in progress");
-
- AZ::Render::FrameCaptureOutcome capOutcome;
- AZ::Render::FrameCaptureRequestBus::BroadcastResult(capOutcome, &AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, screenshotFilePath);
- if (!capOutcome.IsSuccess())
- {
- ReportScriptError(AZStd::string::format("Failed to initiate screenshot capture for '%s: %s'", screenshotFilePath.c_str(), capOutcome.GetError().m_errorMessage.c_str()));
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- s_instance->m_frameCaptureId = capOutcome.GetValue();
- s_instance->AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect(s_instance->m_frameCaptureId);
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- s_instance->m_scriptOperations.push([]()
- {
- s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
- });
- // restore imgui show/hide
- s_instance->m_scriptOperations.push([]()
- {
- s_instance->SetShowImGui(s_instance->m_prevShowImGui);
- });
- }
- void ScriptManager::Script_CaptureScreenshotWithImGui(const AZStd::string& imageName)
- {
- Script_SetShowImGui(true);
- auto operation = [imageName]()
- {
- // Note this will pause the script until the capture is complete
- if (PrepareForScreenCapture(imageName))
- {
- AZ::Render::FrameCapturePathOutcome pathOutcome;
- AZ::Render::FrameCaptureTestRequestBus::BroadcastResult(
- pathOutcome,
- &AZ::Render::FrameCaptureTestRequestBus::Events::BuildScreenshotFilePath,
- imageName, true);
- if (!pathOutcome.IsSuccess())
- {
- ReportScriptError(pathOutcome.GetError().m_errorMessage);
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- AZStd::string screenshotFilePath = pathOutcome.GetValue();
- AZ_Assert(s_instance->m_frameCaptureId == AZ::Render::InvalidFrameCaptureId, "Attempting to start a capture while one is in progress");
-
- AZ::Render::FrameCaptureOutcome capOutcome;
- AZ::Render::FrameCaptureRequestBus::BroadcastResult(capOutcome, &AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, screenshotFilePath);
- if (!capOutcome.IsSuccess())
- {
- ReportScriptError(AZStd::string::format("Failed to initiate screenshot capture for '%s: %s'", screenshotFilePath.c_str(), capOutcome.GetError().m_errorMessage.c_str()));
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- s_instance->m_frameCaptureId = capOutcome.GetValue();
- s_instance->AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect(s_instance->m_frameCaptureId);
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- s_instance->m_scriptOperations.push([]()
- {
- s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
- });
- // restore imgui show/hide
- s_instance->m_scriptOperations.push([]()
- {
- s_instance->SetShowImGui(s_instance->m_prevShowImGui);
- });
- }
- void ScriptManager::Script_CaptureScreenshotWithPreview(const AZStd::string& imageName)
- {
- auto operation = [imageName]()
- {
- // Note this will pause the script until the capture is complete
- if (PrepareForScreenCapture(imageName))
- {
- AZ::Render::FrameCapturePathOutcome pathOutcome;
- AZ::Render::FrameCaptureTestRequestBus::BroadcastResult(
- pathOutcome,
- &AZ::Render::FrameCaptureTestRequestBus::Events::BuildScreenshotFilePath,
- imageName, true);
- if (!pathOutcome.IsSuccess())
- {
- ReportScriptError(pathOutcome.GetError().m_errorMessage);
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- AZStd::string screenshotFilePath = pathOutcome.GetValue();
- AZ_Assert(s_instance->m_frameCaptureId == AZ::Render::InvalidFrameCaptureId, "Attempting to start a capture while one is in progress");
-
- AZ::Render::FrameCaptureOutcome capOutcome;
- AZ::Render::FrameCaptureRequestBus::BroadcastResult(capOutcome, &AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshotWithPreview, screenshotFilePath);
- if (!capOutcome.IsSuccess())
- {
- ReportScriptError(AZStd::string::format("Failed to initiate screenshot capture for '%s: %s'", screenshotFilePath.c_str(), capOutcome.GetError().m_errorMessage.c_str()));
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- s_instance->m_frameCaptureId = capOutcome.GetValue();
- s_instance->AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect(s_instance->m_frameCaptureId);
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- s_instance->m_scriptOperations.push([]()
- {
- s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
- });
- }
- void ScriptManager::Script_CapturePassAttachment(AZ::ScriptDataContext& dc)
- {
- if (dc.GetNumArguments() != 3 && dc.GetNumArguments() != 4)
- {
- ReportScriptError("CapturePassAttachment needs three or four arguments");
- return;
- }
- if (!dc.IsTable(0))
- {
- ReportScriptError("CapturePassAttachment's first argument must be a table of strings");
- return;
- }
- if (!dc.IsString(1) || !dc.IsString(2))
- {
- ReportScriptError("CapturePassAttachment's second and third argument must be strings");
- return;
- }
- if (dc.GetNumArguments() == 4 && !dc.IsString(3))
- {
- ReportScriptError("CapturePassAttachment's forth argument must be a string 'Input' or 'Output'");
- return;
- }
- const char* stringValue = nullptr;
- AZStd::vector<AZStd::string> passHierarchy;
- AZStd::string slot;
- AZStd::string imageName;
- // read slot name and output file path
- dc.ReadArg(1, stringValue);
- slot = AZStd::string(stringValue);
- dc.ReadArg(2, stringValue);
- imageName = AZStd::string(stringValue);
- AZ::RPI::PassAttachmentReadbackOption readbackOption = AZ::RPI::PassAttachmentReadbackOption::Output;
- if (dc.GetNumArguments() == 4)
- {
- AZStd::string option;
- dc.ReadArg(3, option);
- if (option == "Input")
- {
- readbackOption = AZ::RPI::PassAttachmentReadbackOption::Input;
- }
- }
- // read pass hierarchy
- AZ::ScriptDataContext stringtable;
- dc.InspectTable(0, stringtable);
- const char* fieldName;
- int fieldIndex;
- int elementIndex;
- while (stringtable.InspectNextElement(elementIndex, fieldName, fieldIndex))
- {
- if (fieldIndex != -1)
- {
- if (!stringtable.IsString(elementIndex))
- {
- ReportScriptError("CapturePassAttachment's first argument must contain only strings");
- return;
- }
- const char* stringTableValue = nullptr;
- if (stringtable.ReadValue(elementIndex, stringTableValue))
- {
- passHierarchy.push_back(stringTableValue);
- }
- }
- }
- auto operation = [passHierarchy, slot, imageName, readbackOption]()
- {
- // Note this will pause the script until the capture is complete
- if (PrepareForScreenCapture(imageName))
- {
- AZ::Render::FrameCapturePathOutcome pathOutcome;
- AZ::Render::FrameCaptureTestRequestBus::BroadcastResult(
- pathOutcome,
- &AZ::Render::FrameCaptureTestRequestBus::Events::BuildScreenshotFilePath,
- imageName, true);
- if (!pathOutcome.IsSuccess())
- {
- ReportScriptError(pathOutcome.GetError().m_errorMessage);
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- AZStd::string screenshotFilePath = pathOutcome.GetValue();
- AZ_Assert(s_instance->m_frameCaptureId == AZ::Render::InvalidFrameCaptureId, "Attempting to start a capture while one is in progress");
- AZ::Render::FrameCaptureOutcome capOutcome;
- AZ::Render::FrameCaptureRequestBus::BroadcastResult(capOutcome, &AZ::Render::FrameCaptureRequestBus::Events::CapturePassAttachment, screenshotFilePath, passHierarchy, slot, readbackOption);
- if (!capOutcome.IsSuccess())
- {
- ReportScriptError(AZStd::string::format("Failed to initiate screenshot capture for '%s: %s'", screenshotFilePath.c_str(), capOutcome.GetError().m_errorMessage.c_str()));
- s_instance->m_isCapturePending = false;
- s_instance->m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- s_instance->ResumeScript();
- return;
- }
- s_instance->m_frameCaptureId = capOutcome.GetValue();
- s_instance->AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect(s_instance->m_frameCaptureId);
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- s_instance->m_scriptOperations.push([]()
- {
- s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
- });
- }
- void ScriptManager::OnFrameCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string &info)
- {
- m_isCapturePending = false;
- m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
- AZ::Render::FrameCaptureNotificationBus::Handler::BusDisconnect();
- ResumeScript();
- // 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
- // alert users to a common issue and what action to take. Any other Format issues will be reported by FrameCaptureSystemComponent with a
- // "Can't save image with format %s to a ppm file" message.
- if (result == AZ::Render::FrameCaptureResult::UnsupportedFormat && info.find(AZ::RHI::ToString(AZ::RHI::Format::R10G10B10A2_UNORM)) != AZStd::string::npos)
- {
- 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.");
- AbortScripts("Script(s) aborted due to HDR configuration.");
- }
- }
- void ScriptManager::OnCaptureQueryTimestampFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
- {
- m_isCapturePending = false;
- AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
- ResumeScript();
- }
- void ScriptManager::OnCaptureCpuFrameTimeFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
- {
- m_isCapturePending = false;
- AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
- ResumeScript();
- }
- void ScriptManager::OnCaptureQueryPipelineStatisticsFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
- {
- m_isCapturePending = false;
- AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
- ResumeScript();
- }
- void ScriptManager::OnCaptureFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
- {
- m_isCapturePending = false;
- AZ::Debug::ProfilerNotificationBus::Handler::BusDisconnect();
- ResumeScript();
- }
- void ScriptManager::OnCaptureBenchmarkMetadataFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
- {
- m_isCapturePending = false;
- AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
- ResumeScript();
- }
- void ScriptManager::Script_CapturePassTimestamp(AZ::ScriptDataContext& dc)
- {
- AZStd::string outputFilePath;
- const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
- if (!readScriptDataContext)
- {
- return;
- }
- auto operation = [outputFilePath]()
- {
- s_instance->m_isCapturePending = true;
- s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
- s_instance->PauseScript();
- AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassTimestamp, outputFilePath);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_CaptureCpuFrameTime(AZ::ScriptDataContext& dc)
- {
- AZStd::string outputFilePath;
- const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
- if (!readScriptDataContext)
- {
- return;
- }
- auto operation = [outputFilePath]()
- {
- s_instance->m_isCapturePending = true;
- s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
- s_instance->PauseScript();
- AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CaptureCpuFrameTime, outputFilePath);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_CapturePassPipelineStatistics(AZ::ScriptDataContext& dc)
- {
- AZStd::string outputFilePath;
- const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
- if (!readScriptDataContext)
- {
- return;
- }
- auto operation = [outputFilePath]()
- {
- s_instance->m_isCapturePending = true;
- s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
- s_instance->PauseScript();
- AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassPipelineStatistics, outputFilePath);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_CaptureCpuProfilingStatistics(AZ::ScriptDataContext& dc)
- {
- AZStd::string outputFilePath;
- const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
- if (!readScriptDataContext)
- {
- return;
- }
- auto operation = [outputFilePath]()
- {
- s_instance->m_isCapturePending = true;
- s_instance->AZ::Debug::ProfilerNotificationBus::Handler::BusConnect();
- s_instance->PauseScript();
- if (auto profilerSystem = AZ::Debug::ProfilerSystemInterface::Get(); profilerSystem)
- {
- profilerSystem->CaptureFrame(outputFilePath);
- }
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_CaptureBenchmarkMetadata(AZ::ScriptDataContext& dc)
- {
- if (dc.GetNumArguments() != 2)
- {
- ReportScriptError("CaptureBenchmarkMetadata needs two arguments, benchmarkName and outputFilePath.");
- return;
- }
- if (!dc.IsString(0) || !dc.IsString(1))
- {
- ReportScriptError("CaptureBenchmarkMetadata's arguments benchmarkName and outputFilePath must both be of type string.");
- return;
- }
- const char* stringValue = nullptr;
- AZStd::string benchmarkName;
- AZStd::string outputFilePath;
- dc.ReadArg(0, stringValue);
- benchmarkName = AZStd::string(stringValue);
- dc.ReadArg(1, stringValue);
- outputFilePath = AZStd::string(stringValue);
- auto operation = [benchmarkName, outputFilePath]()
- {
- s_instance->m_isCapturePending = true;
- s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
- s_instance->PauseScript();
- AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CaptureBenchmarkMetadata, benchmarkName, outputFilePath);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- bool ScriptManager::ValidateProfilingCaptureScripContexts(AZ::ScriptDataContext& dc, AZStd::string& outputFilePath)
- {
- if (dc.GetNumArguments() != 1)
- {
- ReportScriptError("ProfilingCaptureScriptDataContext needs one argument");
- return false;
- }
- if (!dc.IsString(0))
- {
- ReportScriptError("ProfilingCaptureScriptDataContext's first (and only) argument must be of type string");
- return false;
- }
- // Read slot name and output file path
- const char* stringValue = nullptr;
- dc.ReadArg(0, stringValue);
- if (stringValue == nullptr)
- {
- ReportScriptError("ProfilingCaptureScriptDataContext failed to read the string value");
- return false;
- }
- outputFilePath = AZStd::string(stringValue);
- return true;
- }
- float ScriptManager::Script_DegToRad(float degrees)
- {
- return AZ::DegToRad(degrees);
- }
- AZStd::string ScriptManager::Script_GetRenderApiName()
- {
- AZ::RPI::RPISystemInterface* rpiSystem = AZ::RPI::RPISystemInterface::Get();
- return rpiSystem->GetRenderApiName().GetCStr();
- }
- int ScriptManager::Script_GetRandomTestSeed()
- {
- return s_instance->m_testSuiteRunConfig.m_randomSeed;
- }
- void ScriptManager::CheckArcBallControllerHandler()
- {
- if (0 == AZ::Debug::ArcBallControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
- {
- ReportScriptError("There is no handler for ArcBallControllerRequestBus for the camera entity.");
- }
- }
- void ScriptManager::CheckNoClipControllerHandler()
- {
- if (0 == AZ::Debug::NoClipControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
- {
- ReportScriptError("There is no handler for NoClipControllerRequestBus for the camera entity.");
- }
- }
- void ScriptManager::Script_ArcBallCameraController_SetCenter(AZ::Vector3 center)
- {
- auto operation = [center]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckArcBallControllerHandler();
- AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_ArcBallCameraController_SetPan(AZ::Vector3 pan)
- {
- auto operation = [pan]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckArcBallControllerHandler();
- AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPan, pan);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_ArcBallCameraController_SetDistance(float distance)
- {
- auto operation = [distance]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckArcBallControllerHandler();
- AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, distance);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_ArcBallCameraController_SetHeading(float heading)
- {
- auto operation = [heading]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckArcBallControllerHandler();
- AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading, heading);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_ArcBallCameraController_SetPitch(float pitch)
- {
- auto operation = [pitch]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckArcBallControllerHandler();
- AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch, pitch);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_NoClipCameraController_SetPosition(AZ::Vector3 position)
- {
- auto operation = [position]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckNoClipControllerHandler();
- AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPosition, position);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_NoClipCameraController_SetHeading(float heading)
- {
- auto operation = [heading]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckNoClipControllerHandler();
- AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading, heading);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_NoClipCameraController_SetPitch(float pitch)
- {
- auto operation = [pitch]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckNoClipControllerHandler();
- AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch, pitch);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_NoClipCameraController_SetFov(float fov)
- {
- auto operation = [fov]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- CheckNoClipControllerHandler();
- AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetFov, fov);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_AssetTracking_Start()
- {
- auto operation = []()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- s_instance->m_assetStatusTracker.StartTracking();
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_AssetTracking_ExpectAsset(const AZStd::string& sourceAssetPath, uint32_t expectedCount)
- {
- auto operation = [sourceAssetPath, expectedCount]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- s_instance->m_assetStatusTracker.ExpectAsset(sourceAssetPath, expectedCount);
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_AssetTracking_IdleUntilExpectedAssetsFinish(float timeout)
- {
- auto operation = [timeout]()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- AZ_Assert(!s_instance->m_waitForAssetTracker, "It shouldn't be possible to run the next command until m_waitForAssetTracker is false");
- s_instance->m_waitForAssetTracker = true;
- s_instance->m_assetTrackingTimeout = timeout;
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- void ScriptManager::Script_AssetTracking_Stop()
- {
- auto operation = []()
- {
- AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
- s_instance->m_assetStatusTracker.StopTracking();
- };
- s_instance->m_scriptOperations.push(AZStd::move(operation));
- }
- } // namespace AtomSampleViewer
|