channel_state_cache.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-FileCopyrightText: 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. #pragma once
  4. #include <deque>
  5. #include <limits>
  6. #include <mutex>
  7. #include <optional>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. namespace Tegra {
  12. namespace Engines {
  13. class Maxwell3D;
  14. class KeplerCompute;
  15. } // namespace Engines
  16. class MemoryManager;
  17. namespace Control {
  18. struct ChannelState;
  19. }
  20. } // namespace Tegra
  21. namespace VideoCommon {
  22. class ChannelInfo {
  23. public:
  24. ChannelInfo() = delete;
  25. explicit ChannelInfo(Tegra::Control::ChannelState& state);
  26. ChannelInfo(const ChannelInfo& state) = delete;
  27. ChannelInfo& operator=(const ChannelInfo&) = delete;
  28. Tegra::Engines::Maxwell3D& maxwell3d;
  29. Tegra::Engines::KeplerCompute& kepler_compute;
  30. Tegra::MemoryManager& gpu_memory;
  31. };
  32. template <class P>
  33. class ChannelSetupCaches {
  34. public:
  35. /// Operations for setting the channel of execution.
  36. virtual ~ChannelSetupCaches();
  37. /// Create channel state.
  38. virtual void CreateChannel(Tegra::Control::ChannelState& channel);
  39. /// Bind a channel for execution.
  40. virtual void BindToChannel(s32 id);
  41. /// Erase channel's state.
  42. void EraseChannel(s32 id);
  43. Tegra::MemoryManager* GetFromID(size_t id) const {
  44. std::unique_lock<std::mutex> lk(config_mutex);
  45. const auto ref = address_spaces.find(id);
  46. return ref->second.gpu_memory;
  47. }
  48. std::optional<size_t> getStorageID(size_t id) const {
  49. std::unique_lock<std::mutex> lk(config_mutex);
  50. const auto ref = address_spaces.find(id);
  51. if (ref == address_spaces.end()) {
  52. return std::nullopt;
  53. }
  54. return ref->second.storage_id;
  55. }
  56. protected:
  57. static constexpr size_t UNSET_CHANNEL{std::numeric_limits<size_t>::max()};
  58. P* channel_state;
  59. size_t current_channel_id{UNSET_CHANNEL};
  60. size_t current_address_space{};
  61. Tegra::Engines::Maxwell3D* maxwell3d;
  62. Tegra::Engines::KeplerCompute* kepler_compute;
  63. Tegra::MemoryManager* gpu_memory;
  64. std::deque<P> channel_storage;
  65. std::deque<size_t> free_channel_ids;
  66. std::unordered_map<s32, size_t> channel_map;
  67. std::vector<size_t> active_channel_ids;
  68. struct AddresSpaceRef {
  69. size_t ref_count;
  70. size_t storage_id;
  71. Tegra::MemoryManager* gpu_memory;
  72. };
  73. std::unordered_map<size_t, AddresSpaceRef> address_spaces;
  74. mutable std::mutex config_mutex;
  75. virtual void OnGPUASRegister([[maybe_unused]] size_t map_id) {}
  76. };
  77. } // namespace VideoCommon