node_states.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "audio_core/renderer/nodes/node_states.h"
  4. #include "common/logging/log.h"
  5. namespace AudioCore::AudioRenderer {
  6. void NodeStates::Initialize(std::span<u8> buffer_, [[maybe_unused]] const u64 node_buffer_size,
  7. const u32 count) {
  8. u64 num_blocks{Common::AlignUp(count, 0x40) / sizeof(u64)};
  9. u64 offset{0};
  10. node_count = count;
  11. nodes_found.buffer.resize(count);
  12. nodes_found.size = count;
  13. nodes_found.reset();
  14. offset += num_blocks;
  15. nodes_complete.buffer.resize(count);
  16. nodes_complete.size = count;
  17. nodes_complete.reset();
  18. offset += num_blocks;
  19. results = {reinterpret_cast<u32*>(&buffer_[offset]), count};
  20. offset += count * sizeof(u32);
  21. stack.stack = {reinterpret_cast<u32*>(&buffer_[offset]), count * count};
  22. stack.size = count * count;
  23. stack.unk_10 = count * count;
  24. offset += count * count * sizeof(u32);
  25. }
  26. bool NodeStates::Tsort(const EdgeMatrix& edge_matrix) {
  27. return DepthFirstSearch(edge_matrix, stack);
  28. }
  29. bool NodeStates::DepthFirstSearch(const EdgeMatrix& edge_matrix, Stack& stack_) {
  30. ResetState();
  31. for (u32 node_id = 0; node_id < node_count; node_id++) {
  32. if (GetState(node_id) == SearchState::Unknown) {
  33. stack_.push(node_id);
  34. }
  35. while (stack_.Count() > 0) {
  36. auto current_node{stack_.top()};
  37. switch (GetState(current_node)) {
  38. case SearchState::Unknown:
  39. SetState(current_node, SearchState::Found);
  40. break;
  41. case SearchState::Found:
  42. SetState(current_node, SearchState::Complete);
  43. PushTsortResult(current_node);
  44. stack_.pop();
  45. continue;
  46. case SearchState::Complete:
  47. stack_.pop();
  48. continue;
  49. }
  50. const auto edge_count{edge_matrix.GetNodeCount()};
  51. for (u32 edge_id = 0; edge_id < edge_count; edge_id++) {
  52. if (!edge_matrix.Connected(current_node, edge_id)) {
  53. continue;
  54. }
  55. switch (GetState(edge_id)) {
  56. case SearchState::Unknown:
  57. stack_.push(edge_id);
  58. break;
  59. case SearchState::Found:
  60. LOG_ERROR(Service_Audio,
  61. "Cycle detected in the node graph, graph is not a DAG! "
  62. "Bailing to avoid an infinite loop");
  63. ResetState();
  64. return false;
  65. case SearchState::Complete:
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. return true;
  72. }
  73. NodeStates::SearchState NodeStates::GetState(const u32 id) const {
  74. if (nodes_found.buffer[id]) {
  75. return SearchState::Found;
  76. } else if (nodes_complete.buffer[id]) {
  77. return SearchState::Complete;
  78. }
  79. return SearchState::Unknown;
  80. }
  81. void NodeStates::PushTsortResult(const u32 id) {
  82. results[result_pos++] = id;
  83. }
  84. void NodeStates::SetState(const u32 id, const SearchState state) {
  85. switch (state) {
  86. case SearchState::Complete:
  87. nodes_found.buffer[id] = false;
  88. nodes_complete.buffer[id] = true;
  89. break;
  90. case SearchState::Found:
  91. nodes_found.buffer[id] = true;
  92. nodes_complete.buffer[id] = false;
  93. break;
  94. case SearchState::Unknown:
  95. nodes_found.buffer[id] = false;
  96. nodes_complete.buffer[id] = false;
  97. break;
  98. default:
  99. LOG_ERROR(Service_Audio, "Unknown node SearchState {}", static_cast<u32>(state));
  100. break;
  101. }
  102. }
  103. void NodeStates::ResetState() {
  104. nodes_found.reset();
  105. nodes_complete.reset();
  106. std::fill(results.begin(), results.end(), -1);
  107. result_pos = 0;
  108. }
  109. u32 NodeStates::GetNodeCount() const {
  110. return node_count;
  111. }
  112. std::pair<std::span<u32>::reverse_iterator, size_t> NodeStates::GetSortedResuls() const {
  113. return {results.rbegin(), result_pos};
  114. }
  115. } // namespace AudioCore::AudioRenderer