SkinnedMeshDebugDisplay.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <CryCommon/IConsole.h>
  9. #include <SkinnedMesh/SkinnedMeshDebugDisplay.h>
  10. #include <Atom/Feature/SkinnedMesh/SkinnedMeshStatsBus.h>
  11. #include <Atom/RPI.Public/Scene.h>
  12. #include <AzFramework/Scene/SceneSystemInterface.h>
  13. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  14. #include <ISystem.h>
  15. namespace AZ
  16. {
  17. namespace Render
  18. {
  19. SkinnedMeshDebugDisplay::SkinnedMeshDebugDisplay()
  20. {
  21. CrySystemEventBus::Handler::BusConnect();
  22. AZ::Render::Bootstrap::NotificationBus::Handler::BusConnect();
  23. }
  24. SkinnedMeshDebugDisplay::~SkinnedMeshDebugDisplay()
  25. {
  26. AZ::Render::Bootstrap::NotificationBus::Handler::BusDisconnect();
  27. CrySystemEventBus::Handler::BusDisconnect();
  28. AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect();
  29. }
  30. void SkinnedMeshDebugDisplay::OnCrySystemInitialized(ISystem& system, [[maybe_unused]] const SSystemInitParams& systemInitParams)
  31. {
  32. // Once CrySystem is initialized, we can register cvars
  33. if (system.GetGlobalEnvironment() && system.GetGlobalEnvironment()->pConsole)
  34. {
  35. system.GetGlobalEnvironment()->pConsole->Register("r_skinnedMeshDisplaySceneStats", &r_skinnedMeshDisplaySceneStats, 0, VF_NULL,
  36. CVARHELP("Enable debug display of skinned mesh stats\n"
  37. " 1 = 'Skinned Mesh Scene Stats': This represents all lods of all the skinned meshes in the scene, not just what's in view."
  38. " Effectively, this is everything that is created and uploaded to the GPU, though only the visible subset of meshes/lods will be skinned.\n"));
  39. }
  40. else
  41. {
  42. AZ_Assert(false, "Attempting to register r_skinnedMeshDisplaySceneStats before the cvar system has been initialized");
  43. }
  44. }
  45. void SkinnedMeshDebugDisplay::OnCrySystemShutdown(ISystem& system)
  46. {
  47. if (system.GetGlobalEnvironment() && system.GetGlobalEnvironment()->pConsole)
  48. {
  49. system.GetGlobalEnvironment()->pConsole->UnregisterVariable("r_skinnedMeshDisplaySceneStats");
  50. }
  51. else
  52. {
  53. AZ_Assert(false, "Attempting to unregister r_skinnedMeshDisplaySceneStats after the cvar system has been shut down");
  54. }
  55. }
  56. void SkinnedMeshDebugDisplay::OnCryEditorInitialized()
  57. {
  58. // Once the editor is initialized, listen to ViewportDebugDisplayEventBus
  59. AzFramework::EntityContextId editorEntityContextId = AzFramework::EntityContextId::CreateNull();
  60. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(editorEntityContextId, &AzToolsFramework::EditorEntityContextRequestBus::Events::GetEditorEntityContextId);
  61. AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect(editorEntityContextId);
  62. }
  63. void SkinnedMeshDebugDisplay::DisplayViewport2d([[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo, AzFramework::DebugDisplayRequests& debugDisplay)
  64. {
  65. if (r_skinnedMeshDisplaySceneStats == 1)
  66. {
  67. // Get scene id when DisplayViewport2d is called. The RPI Scene may not be ready when OnCryEditorInitialized was called
  68. SkinnedMeshSceneStats stats{};
  69. SkinnedMeshStatsRequestBus::EventResult(stats, m_sceneId, &SkinnedMeshStatsRequestBus::Events::GetSceneStats);
  70. float x = 0;
  71. float y = 16.0f; // By default, the editor's debug display text shows the number of entities in the top left corner. Offset so the skinned mesh stats don't overlap
  72. float size = 1.25f;
  73. bool center = false;
  74. AZStd::string debugString = AZStd::string::format(
  75. "Skinned Mesh Scene Stats:\n"
  76. " SkinnedMeshRenderProxy count: %zu\n"
  77. " DispatchItem count: %zu\n"
  78. " Bone count: %zu\n"
  79. " Vertex count: %zu\n",
  80. stats.skinnedMeshRenderProxyCount, stats.dispatchItemCount, stats.boneCount, stats.vertexCount
  81. );
  82. debugDisplay.Draw2dTextLabel(x, y, size, debugString.c_str(), center);
  83. }
  84. }
  85. void SkinnedMeshDebugDisplay::OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene)
  86. {
  87. m_sceneId = bootstrapScene->GetId();
  88. }
  89. }// namespace Render
  90. }// namespace AZ