DebugOutput.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "DebugOutput.h"
  9. #include <AzCore/std/optional.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/Serialization/Json/JsonUtils.h>
  12. #include <AzCore/Serialization/Utils.h>
  13. #include <AzFramework/StringFunc/StringFunc.h>
  14. #include <SceneAPI/SceneCore/Containers/Scene.h>
  15. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  16. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  17. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  18. #include <SceneAPI/SceneCore/Events/ExportProductList.h>
  19. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  20. namespace AZ::SceneAPI::Utilities
  21. {
  22. bool SaveToJson(const AZStd::string& fileName, const DebugSceneGraph& graph);
  23. void DebugNode::Reflect(AZ::ReflectContext* context)
  24. {
  25. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  26. if (serialize)
  27. {
  28. serialize->Class<DebugNode>()
  29. ->Field("Name", &DebugNode::m_name)
  30. ->Field("Path", &DebugNode::m_path)
  31. ->Field("Type", &DebugNode::m_type)
  32. ->Field("Data", &DebugNode::m_data);
  33. }
  34. }
  35. void DebugSceneGraph::Reflect(AZ::ReflectContext* context)
  36. {
  37. DebugNode::Reflect(context);
  38. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  39. if (serialize)
  40. {
  41. serialize->Class<DebugSceneGraph>()
  42. ->Field("Version", &DebugSceneGraph::m_version)
  43. ->Field("ProductName", &DebugSceneGraph::m_productName)
  44. ->Field("SceneName", &DebugSceneGraph::m_sceneName)
  45. ->Field("Nodes", &DebugSceneGraph::m_nodes);
  46. }
  47. }
  48. void DebugOutput::Write(const char* name, const char* data)
  49. {
  50. m_output += AZStd::string::format("\t%s: %s\n", name, data);
  51. }
  52. void DebugOutput::WriteArray(const char* name, const unsigned int* data, int size)
  53. {
  54. m_output += AZStd::string::format("\t%s: ", name);
  55. for (int index = 0; index < size; ++index)
  56. {
  57. m_output += AZStd::string::format("%d, ", data[index]);
  58. }
  59. m_output += AZStd::string::format("\n");
  60. }
  61. void DebugOutput::Write(const char* name, const AZStd::string& data)
  62. {
  63. Write(name, data.c_str());
  64. AddToNode(name, data);
  65. }
  66. void DebugOutput::Write(const char* name, double data)
  67. {
  68. m_output += AZStd::string::format("\t%s: %f\n", name, data);
  69. AddToNode(name, data);
  70. }
  71. void DebugOutput::Write(const char* name, uint64_t data)
  72. {
  73. AZ::u64 multiplatformSafeData(static_cast<AZ::u64>(data));
  74. m_output += AZStd::string::format("\t%s: %llu\n", name, multiplatformSafeData);
  75. AddToNode(name, multiplatformSafeData);
  76. }
  77. void DebugOutput::Write(const char* name, int64_t data)
  78. {
  79. AZ::s64 multiplatformSafeData(static_cast<AZ::s64>(data));
  80. m_output += AZStd::string::format("\t%s: %lld\n", name, multiplatformSafeData);
  81. AddToNode(name, multiplatformSafeData);
  82. }
  83. void DebugOutput::Write(const char* name, const DataTypes::MatrixType& data)
  84. {
  85. AZ::Vector3 basisX{};
  86. AZ::Vector3 basisY{};
  87. AZ::Vector3 basisZ{};
  88. AZ::Vector3 translation{};
  89. data.GetBasisAndTranslation(&basisX, &basisY, &basisZ, &translation);
  90. m_pauseNodeData = true;
  91. m_output += AZStd::string::format("\t%s:\n", name);
  92. m_output += "\t";
  93. Write("BasisX", basisX);
  94. m_output += "\t";
  95. Write("BasisY", basisY);
  96. m_output += "\t";
  97. Write("BasisZ", basisZ);
  98. m_output += "\t";
  99. Write("Transl", translation);
  100. m_pauseNodeData = false;
  101. AddToNode(name, data);
  102. }
  103. void DebugOutput::Write(const char* name, bool data)
  104. {
  105. m_output += AZStd::string::format("\t%s: %s\n", name, data ? "true" : "false");
  106. AddToNode(name, data);
  107. }
  108. void DebugOutput::Write(const char* name, Vector3 data)
  109. {
  110. m_output += AZStd::string::format("\t%s: <% f, % f, % f>\n", name, data.GetX(), data.GetY(), data.GetZ());
  111. AddToNode(name, data);
  112. }
  113. void DebugOutput::Write(const char* name, AZStd::optional<bool> data)
  114. {
  115. if (data.has_value())
  116. {
  117. Write(name, data.value());
  118. }
  119. else
  120. {
  121. Write(name, "Not set");
  122. }
  123. }
  124. void DebugOutput::Write(const char* name, AZStd::optional<float> data)
  125. {
  126. if (data.has_value())
  127. {
  128. Write(name, data.value());
  129. }
  130. else
  131. {
  132. Write(name, "Not set");
  133. }
  134. }
  135. void DebugOutput::Write(const char* name, AZStd::optional<AZ::Vector3> data)
  136. {
  137. if (data.has_value())
  138. {
  139. Write(name, data.value());
  140. }
  141. else
  142. {
  143. Write(name, "Not set");
  144. }
  145. }
  146. const AZStd::string& DebugOutput::GetOutput() const
  147. {
  148. return m_output;
  149. }
  150. DebugNode DebugOutput::GetDebugNode() const
  151. {
  152. return m_currentNode;
  153. }
  154. void WriteAndLog(AZ::IO::SystemFile& dbgFile, const char* strToWrite)
  155. {
  156. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow, "%s", strToWrite);
  157. dbgFile.Write(strToWrite, strlen(strToWrite));
  158. dbgFile.Write("\n", strlen("\n"));
  159. }
  160. void DebugOutput::BuildDebugSceneGraph(const char* outputFolder, AZ::SceneAPI::Events::ExportProductList& productList, const AZStd::shared_ptr<AZ::SceneAPI::Containers::Scene>& scene, AZStd::string productName)
  161. {
  162. AZStd::string debugSceneFile;
  163. AzFramework::StringFunc::Path::ConstructFull(outputFolder, productName.c_str(), debugSceneFile);
  164. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow, "outputFolder %s, name %s.\n", outputFolder, productName.c_str());
  165. AZ::IO::SystemFile dbgFile;
  166. if (dbgFile.Open(debugSceneFile.c_str(), AZ::IO::SystemFile::SF_OPEN_CREATE | AZ::IO::SystemFile::SF_OPEN_WRITE_ONLY))
  167. {
  168. WriteAndLog(dbgFile, AZStd::string::format("ProductName: %s", productName.c_str()).c_str());
  169. WriteAndLog(dbgFile, AZStd::string::format("debugSceneGraphVersion: %d", SceneGraphVersion).c_str());
  170. WriteAndLog(dbgFile, scene->GetName().c_str());
  171. const AZ::SceneAPI::Containers::SceneGraph& sceneGraph = scene->GetGraph();
  172. auto names = sceneGraph.GetNameStorage();
  173. auto content = sceneGraph.GetContentStorage();
  174. auto pairView = AZ::SceneAPI::Containers::Views::MakePairView(names, content);
  175. auto view = AZ::SceneAPI::Containers::Views::MakeSceneGraphDownwardsView<
  176. AZ::SceneAPI::Containers::Views::BreadthFirst>(
  177. sceneGraph, sceneGraph.GetRoot(), pairView.cbegin(), true);
  178. DebugSceneGraph debugSceneGraph;
  179. debugSceneGraph.m_version = SceneGraphVersion;
  180. debugSceneGraph.m_productName = productName;
  181. debugSceneGraph.m_sceneName = scene->GetName().c_str();
  182. for (auto&& viewIt : view)
  183. {
  184. if (viewIt.second == nullptr)
  185. {
  186. continue;
  187. }
  188. AZ::SceneAPI::DataTypes::IGraphObject* graphObject = const_cast<AZ::SceneAPI::DataTypes::IGraphObject*>(viewIt.second.get());
  189. WriteAndLog(dbgFile, AZStd::string::format("Node Name: %s", viewIt.first.GetName()).c_str());
  190. WriteAndLog(dbgFile, AZStd::string::format("Node Path: %s", viewIt.first.GetPath()).c_str());
  191. WriteAndLog(dbgFile, AZStd::string::format("Node Type: %s", graphObject->RTTI_GetTypeName()).c_str());
  192. AZ::SceneAPI::Utilities::DebugOutput debugOutput(
  193. DebugNode(viewIt.first.GetName(), viewIt.first.GetPath(), graphObject->RTTI_GetTypeName()));
  194. viewIt.second->GetDebugOutput(debugOutput);
  195. if (!debugOutput.GetOutput().empty())
  196. {
  197. WriteAndLog(dbgFile, debugOutput.GetOutput().c_str());
  198. }
  199. debugSceneGraph.m_nodes.push_back(debugOutput.GetDebugNode());
  200. }
  201. dbgFile.Close();
  202. // XML is useful because it stores more information than JSON with the serializer, so some automation is better suited to use XML.
  203. Utils::SaveObjectToFile((debugSceneFile + ".xml").c_str(), DataStream::StreamType::ST_XML, &debugSceneGraph);
  204. // JSON is useful because it can be quicker and easier to parse than XML, and more structured than the human readable dbgsg file.
  205. AZStd::string jsonFileName(debugSceneFile + ".json");
  206. SaveToJson(jsonFileName, debugSceneGraph);
  207. static const AZ::Data::AssetType dbgSceneGraphAssetType("{07F289D1-4DC7-4C40-94B4-0A53BBCB9F0B}");
  208. productList.AddProduct(productName, AZ::Uuid::CreateName(productName.c_str()), dbgSceneGraphAssetType,
  209. AZStd::nullopt, AZStd::nullopt);
  210. static const AZ::Data::AssetType dbgSceneGraphXmlAssetType("{51F37614-0D77-4F36-9AC6-7ED70A0AC868}");
  211. productList.AddProduct(
  212. (productName + ".xml"), AZ::Uuid::CreateName((productName + ".xml").c_str()), dbgSceneGraphXmlAssetType,
  213. AZStd::nullopt, AZStd::nullopt);
  214. static const AZ::Data::AssetType dbgSceneGraphJsonAssetType("{4342B27E-0E14-49C3-B3B9-BCDB9A5FCA23}");
  215. productList.AddProduct(
  216. jsonFileName, AZ::Uuid::CreateName((productName + ".json").c_str()), dbgSceneGraphJsonAssetType,
  217. AZStd::nullopt, AZStd::nullopt);
  218. // save out debug text for the Scene Manifest
  219. AZStd::string productNameDebugManifest { debugSceneFile };
  220. AzFramework::StringFunc::Path::ReplaceExtension(productNameDebugManifest, "assetinfo.dbg");
  221. scene->GetManifest().SaveToFile(productNameDebugManifest.c_str());
  222. static const AZ::Data::AssetType dbgSceneManifestAssetType("{48A78BE7-B3F2-44B8-8AA6-F0607E9A75A5}");
  223. productList.AddProduct(
  224. productNameDebugManifest,
  225. AZ::Uuid::CreateName((productName + ".assetinfo.dbg").c_str()),
  226. dbgSceneManifestAssetType,
  227. AZStd::nullopt,
  228. AZStd::nullopt);
  229. }
  230. }
  231. bool SaveToJson(const AZStd::string& fileName, const DebugSceneGraph& graph)
  232. {
  233. AZ::JsonSerializerSettings settings;
  234. rapidjson::Document jsonDocument;
  235. auto jsonResult = JsonSerialization::Store(jsonDocument, jsonDocument.GetAllocator(), graph, settings);
  236. if (jsonResult.GetProcessing() == AZ::JsonSerializationResult::Processing::Halted)
  237. {
  238. AZ_Error("Scene Debug Output", false,
  239. AZStd::string::format(
  240. "JSON serialization of file %.*s failed: %.*s", AZ_STRING_ARG(fileName), AZ_STRING_ARG(jsonResult.ToString(""))).c_str());
  241. return false;
  242. }
  243. auto jsonSaveResult = AZ::JsonSerializationUtils::WriteJsonFile(jsonDocument, fileName);
  244. AZ_Error(
  245. "Scene Debug Output",
  246. jsonSaveResult.IsSuccess(),
  247. AZStd::string::format("Saving JSON to file %.*s failed: %.*s", AZ_STRING_ARG(fileName), AZ_STRING_ARG(jsonSaveResult.GetError())).c_str());
  248. return jsonSaveResult.IsSuccess();
  249. }
  250. }