shader_environment.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2021 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 <filesystem>
  7. #include <iosfwd>
  8. #include <limits>
  9. #include <memory>
  10. #include <optional>
  11. #include <span>
  12. #include <stop_token>
  13. #include <type_traits>
  14. #include <unordered_map>
  15. #include <vector>
  16. #include "common/common_types.h"
  17. #include "common/unique_function.h"
  18. #include "shader_recompiler/environment.h"
  19. #include "video_core/engines/maxwell_3d.h"
  20. namespace Tegra {
  21. class Memorymanager;
  22. }
  23. namespace VideoCommon {
  24. class GenericEnvironment : public Shader::Environment {
  25. public:
  26. explicit GenericEnvironment() = default;
  27. explicit GenericEnvironment(Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  28. u32 start_address_);
  29. ~GenericEnvironment() override;
  30. [[nodiscard]] u32 TextureBoundBuffer() const final;
  31. [[nodiscard]] u32 LocalMemorySize() const final;
  32. [[nodiscard]] u32 SharedMemorySize() const final;
  33. [[nodiscard]] std::array<u32, 3> WorkgroupSize() const final;
  34. [[nodiscard]] u64 ReadInstruction(u32 address) final;
  35. [[nodiscard]] std::optional<u64> Analyze();
  36. void SetCachedSize(size_t size_bytes);
  37. [[nodiscard]] size_t CachedSize() const noexcept;
  38. [[nodiscard]] size_t ReadSize() const noexcept;
  39. [[nodiscard]] bool CanBeSerialized() const noexcept;
  40. [[nodiscard]] u64 CalculateHash() const;
  41. void Serialize(std::ofstream& file) const;
  42. protected:
  43. std::optional<u64> TryFindSize();
  44. Shader::TextureType ReadTextureTypeImpl(GPUVAddr tic_addr, u32 tic_limit, bool via_header_index,
  45. u32 raw);
  46. Tegra::MemoryManager* gpu_memory{};
  47. GPUVAddr program_base{};
  48. std::vector<u64> code;
  49. std::unordered_map<u32, Shader::TextureType> texture_types;
  50. std::unordered_map<u64, u32> cbuf_values;
  51. u32 local_memory_size{};
  52. u32 texture_bound{};
  53. u32 shared_memory_size{};
  54. std::array<u32, 3> workgroup_size{};
  55. u32 read_lowest = std::numeric_limits<u32>::max();
  56. u32 read_highest = 0;
  57. u32 cached_lowest = std::numeric_limits<u32>::max();
  58. u32 cached_highest = 0;
  59. bool has_unbound_instructions = false;
  60. };
  61. class GraphicsEnvironment final : public GenericEnvironment {
  62. public:
  63. explicit GraphicsEnvironment() = default;
  64. explicit GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
  65. Tegra::MemoryManager& gpu_memory_,
  66. Tegra::Engines::Maxwell3D::Regs::ShaderProgram program,
  67. GPUVAddr program_base_, u32 start_address_);
  68. ~GraphicsEnvironment() override = default;
  69. u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  70. Shader::TextureType ReadTextureType(u32 handle) override;
  71. private:
  72. Tegra::Engines::Maxwell3D* maxwell3d{};
  73. size_t stage_index{};
  74. };
  75. class ComputeEnvironment final : public GenericEnvironment {
  76. public:
  77. explicit ComputeEnvironment() = default;
  78. explicit ComputeEnvironment(Tegra::Engines::KeplerCompute& kepler_compute_,
  79. Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  80. u32 start_address_);
  81. ~ComputeEnvironment() override = default;
  82. u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  83. Shader::TextureType ReadTextureType(u32 handle) override;
  84. private:
  85. Tegra::Engines::KeplerCompute* kepler_compute{};
  86. };
  87. class FileEnvironment final : public Shader::Environment {
  88. public:
  89. FileEnvironment() = default;
  90. ~FileEnvironment() override = default;
  91. FileEnvironment& operator=(FileEnvironment&&) noexcept = default;
  92. FileEnvironment(FileEnvironment&&) noexcept = default;
  93. FileEnvironment& operator=(const FileEnvironment&) = delete;
  94. FileEnvironment(const FileEnvironment&) = delete;
  95. void Deserialize(std::ifstream& file);
  96. [[nodiscard]] u64 ReadInstruction(u32 address) override;
  97. [[nodiscard]] u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  98. [[nodiscard]] Shader::TextureType ReadTextureType(u32 handle) override;
  99. [[nodiscard]] u32 LocalMemorySize() const override;
  100. [[nodiscard]] u32 SharedMemorySize() const override;
  101. [[nodiscard]] u32 TextureBoundBuffer() const override;
  102. [[nodiscard]] std::array<u32, 3> WorkgroupSize() const override;
  103. private:
  104. std::unique_ptr<u64[]> code;
  105. std::unordered_map<u32, Shader::TextureType> texture_types;
  106. std::unordered_map<u64, u32> cbuf_values;
  107. std::array<u32, 3> workgroup_size{};
  108. u32 local_memory_size{};
  109. u32 shared_memory_size{};
  110. u32 texture_bound{};
  111. u32 read_lowest{};
  112. u32 read_highest{};
  113. };
  114. void SerializePipeline(std::span<const char> key, std::span<const GenericEnvironment* const> envs,
  115. const std::filesystem::path& filename, u32 cache_version);
  116. template <typename Key, typename Envs>
  117. void SerializePipeline(const Key& key, const Envs& envs, const std::filesystem::path& filename,
  118. u32 cache_version) {
  119. static_assert(std::is_trivially_copyable_v<Key>);
  120. static_assert(std::has_unique_object_representations_v<Key>);
  121. SerializePipeline(std::span(reinterpret_cast<const char*>(&key), sizeof(key)),
  122. std::span(envs.data(), envs.size()), filename, cache_version);
  123. }
  124. void LoadPipelines(
  125. std::stop_token stop_loading, const std::filesystem::path& filename, u32 expected_cache_version,
  126. Common::UniqueFunction<void, std::ifstream&, FileEnvironment> load_compute,
  127. Common::UniqueFunction<void, std::ifstream&, std::vector<FileEnvironment>> load_graphics);
  128. } // namespace VideoCommon