shader_cache.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <memory>
  6. #include <mutex>
  7. #include <span>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. #include "common/polyfill_ranges.h"
  13. #include "video_core/control/channel_state_cache.h"
  14. #include "video_core/rasterizer_interface.h"
  15. #include "video_core/shader_environment.h"
  16. namespace Tegra {
  17. class MemoryManager;
  18. }
  19. namespace Tegra::Control {
  20. struct ChannelState;
  21. }
  22. namespace VideoCommon {
  23. class GenericEnvironment;
  24. struct ShaderInfo {
  25. u64 unique_hash{};
  26. size_t size_bytes{};
  27. };
  28. class ShaderCache : public VideoCommon::ChannelSetupCaches<VideoCommon::ChannelInfo> {
  29. static constexpr u64 YUZU_PAGEBITS = 14;
  30. static constexpr u64 YUZU_PAGESIZE = u64(1) << YUZU_PAGEBITS;
  31. static constexpr size_t NUM_PROGRAMS = 6;
  32. struct Entry {
  33. VAddr addr_start;
  34. VAddr addr_end;
  35. ShaderInfo* data;
  36. bool is_memory_marked = true;
  37. bool Overlaps(VAddr start, VAddr end) const noexcept {
  38. return start < addr_end && addr_start < end;
  39. }
  40. };
  41. public:
  42. /// @brief Removes shaders inside a given region
  43. /// @note Checks for ranges
  44. /// @param addr Start address of the invalidation
  45. /// @param size Number of bytes of the invalidation
  46. void InvalidateRegion(VAddr addr, size_t size);
  47. /// @brief Unmarks a memory region as cached and marks it for removal
  48. /// @param addr Start address of the CPU write operation
  49. /// @param size Number of bytes of the CPU write operation
  50. void OnCPUWrite(VAddr addr, size_t size);
  51. /// @brief Flushes delayed removal operations
  52. void SyncGuestHost();
  53. protected:
  54. struct GraphicsEnvironments {
  55. std::array<GraphicsEnvironment, NUM_PROGRAMS> envs;
  56. std::array<Shader::Environment*, NUM_PROGRAMS> env_ptrs;
  57. std::span<Shader::Environment* const> Span() const noexcept {
  58. return std::span(env_ptrs.begin(), std::ranges::find(env_ptrs, nullptr));
  59. }
  60. };
  61. explicit ShaderCache(VideoCore::RasterizerInterface& rasterizer_);
  62. /// @brief Update the hashes and information of shader stages
  63. /// @param unique_hashes Shader hashes to store into when a stage is enabled
  64. /// @return True no success, false on error
  65. bool RefreshStages(std::array<u64, NUM_PROGRAMS>& unique_hashes);
  66. /// @brief Returns information about the current compute shader
  67. /// @return Pointer to a valid shader, nullptr on error
  68. const ShaderInfo* ComputeShader();
  69. /// @brief Collect the current graphics environments
  70. void GetGraphicsEnvironments(GraphicsEnvironments& result,
  71. const std::array<u64, NUM_PROGRAMS>& unique_hashes);
  72. std::array<const ShaderInfo*, NUM_PROGRAMS> shader_infos{};
  73. bool last_shaders_valid = false;
  74. private:
  75. /// @brief Tries to obtain a cached shader starting in a given address
  76. /// @note Doesn't check for ranges, the given address has to be the start of the shader
  77. /// @param addr Start address of the shader, this doesn't cache for region
  78. /// @return Pointer to a valid shader, nullptr when nothing is found
  79. ShaderInfo* TryGet(VAddr addr) const;
  80. /// @brief Register in the cache a given entry
  81. /// @param data Shader to store in the cache
  82. /// @param addr Start address of the shader that will be registered
  83. /// @param size Size in bytes of the shader
  84. void Register(std::unique_ptr<ShaderInfo> data, VAddr addr, size_t size);
  85. /// @brief Invalidate pages in a given region
  86. /// @pre invalidation_mutex is locked
  87. void InvalidatePagesInRegion(VAddr addr, size_t size);
  88. /// @brief Remove shaders marked for deletion
  89. /// @pre invalidation_mutex is locked
  90. void RemovePendingShaders();
  91. /// @brief Invalidates entries in a given range for the passed page
  92. /// @param entries Vector of entries in the page, it will be modified on overlaps
  93. /// @param addr Start address of the invalidation
  94. /// @param addr_end Non-inclusive end address of the invalidation
  95. /// @pre invalidation_mutex is locked
  96. void InvalidatePageEntries(std::vector<Entry*>& entries, VAddr addr, VAddr addr_end);
  97. /// @brief Removes all references to an entry in the invalidation cache
  98. /// @param entry Entry to remove from the invalidation cache
  99. /// @pre invalidation_mutex is locked
  100. void RemoveEntryFromInvalidationCache(const Entry* entry);
  101. /// @brief Unmarks an entry from the rasterizer cache
  102. /// @param entry Entry to unmark from memory
  103. void UnmarkMemory(Entry* entry);
  104. /// @brief Removes a vector of shaders from a list
  105. /// @param removed_shaders Shaders to be removed from the storage
  106. /// @pre invalidation_mutex is locked
  107. /// @pre lookup_mutex is locked
  108. void RemoveShadersFromStorage(std::span<ShaderInfo*> removed_shaders);
  109. /// @brief Creates a new entry in the lookup cache and returns its pointer
  110. /// @pre lookup_mutex is locked
  111. Entry* NewEntry(VAddr addr, VAddr addr_end, ShaderInfo* data);
  112. /// @brief Create a new shader entry and register it
  113. const ShaderInfo* MakeShaderInfo(GenericEnvironment& env, VAddr cpu_addr);
  114. VideoCore::RasterizerInterface& rasterizer;
  115. mutable std::mutex lookup_mutex;
  116. std::mutex invalidation_mutex;
  117. std::unordered_map<u64, std::unique_ptr<Entry>> lookup_cache;
  118. std::unordered_map<u64, std::vector<Entry*>> invalidation_cache;
  119. std::vector<std::unique_ptr<ShaderInfo>> storage;
  120. std::vector<Entry*> marked_for_removal;
  121. };
  122. } // namespace VideoCommon