channel_state_cache.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ChannelInfo(ChannelInfo&& other) = default;
  29. ChannelInfo& operator=(ChannelInfo&& other) = default;
  30. Tegra::Engines::Maxwell3D& maxwell3d;
  31. Tegra::Engines::KeplerCompute& kepler_compute;
  32. Tegra::MemoryManager& gpu_memory;
  33. };
  34. template <class P>
  35. class ChannelSetupCaches {
  36. public:
  37. /// Operations for seting the channel of execution.
  38. virtual ~ChannelSetupCaches();
  39. /// Create channel state.
  40. virtual void CreateChannel(Tegra::Control::ChannelState& channel);
  41. /// Bind a channel for execution.
  42. void BindToChannel(s32 id);
  43. /// Erase channel's state.
  44. void EraseChannel(s32 id);
  45. Tegra::MemoryManager* GetFromID(size_t id) const {
  46. std::unique_lock<std::mutex> lk(config_mutex);
  47. const auto ref = address_spaces.find(id);
  48. return ref->second.gpu_memory;
  49. }
  50. std::optional<size_t> getStorageID(size_t id) const {
  51. std::unique_lock<std::mutex> lk(config_mutex);
  52. const auto ref = address_spaces.find(id);
  53. if (ref == address_spaces.end()) {
  54. return std::nullopt;
  55. }
  56. return ref->second.storage_id;
  57. }
  58. protected:
  59. static constexpr size_t UNSET_CHANNEL{std::numeric_limits<size_t>::max()};
  60. P* channel_state;
  61. size_t current_channel_id{UNSET_CHANNEL};
  62. size_t current_address_space{};
  63. Tegra::Engines::Maxwell3D* maxwell3d;
  64. Tegra::Engines::KeplerCompute* kepler_compute;
  65. Tegra::MemoryManager* gpu_memory;
  66. std::deque<P> channel_storage;
  67. std::deque<size_t> free_channel_ids;
  68. std::unordered_map<s32, size_t> channel_map;
  69. std::vector<size_t> active_channel_ids;
  70. struct AddresSpaceRef {
  71. size_t ref_count;
  72. size_t storage_id;
  73. Tegra::MemoryManager* gpu_memory;
  74. };
  75. std::unordered_map<size_t, AddresSpaceRef> address_spaces;
  76. mutable std::mutex config_mutex;
  77. virtual void OnGPUASRegister([[maybe_unused]] size_t map_id) {}
  78. };
  79. } // namespace VideoCommon