shader_cache.h 5.7 KB

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