shader_cache.h 5.7 KB

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