shader_environment.h 5.6 KB

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