shader_environment.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <filesystem>
  6. #include <iosfwd>
  7. #include <limits>
  8. #include <memory>
  9. #include <optional>
  10. #include <span>
  11. #include <stop_token>
  12. #include <type_traits>
  13. #include <unordered_map>
  14. #include <vector>
  15. #include "common/common_types.h"
  16. #include "common/unique_function.h"
  17. #include "shader_recompiler/environment.h"
  18. #include "video_core/engines/maxwell_3d.h"
  19. namespace Tegra {
  20. class Memorymanager;
  21. }
  22. namespace VideoCommon {
  23. class GenericEnvironment : public Shader::Environment {
  24. public:
  25. explicit GenericEnvironment() = default;
  26. explicit GenericEnvironment(Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  27. u32 start_address_);
  28. ~GenericEnvironment() override;
  29. [[nodiscard]] u32 TextureBoundBuffer() const final;
  30. [[nodiscard]] u32 LocalMemorySize() const final;
  31. [[nodiscard]] u32 SharedMemorySize() const final;
  32. [[nodiscard]] std::array<u32, 3> WorkgroupSize() const final;
  33. [[nodiscard]] u64 ReadInstruction(u32 address) final;
  34. [[nodiscard]] std::optional<u64> Analyze();
  35. void SetCachedSize(size_t size_bytes);
  36. [[nodiscard]] size_t CachedSize() const noexcept;
  37. [[nodiscard]] size_t ReadSize() const noexcept;
  38. [[nodiscard]] bool CanBeSerialized() const noexcept;
  39. [[nodiscard]] u64 CalculateHash() const;
  40. void Dump(u64 hash) override;
  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. u32 initial_offset = 0;
  60. bool has_unbound_instructions = false;
  61. };
  62. class GraphicsEnvironment final : public GenericEnvironment {
  63. public:
  64. explicit GraphicsEnvironment() = default;
  65. explicit GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
  66. Tegra::MemoryManager& gpu_memory_,
  67. Tegra::Engines::Maxwell3D::Regs::ShaderProgram program,
  68. GPUVAddr program_base_, u32 start_address_);
  69. ~GraphicsEnvironment() override = default;
  70. u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  71. Shader::TextureType ReadTextureType(u32 handle) override;
  72. private:
  73. Tegra::Engines::Maxwell3D* maxwell3d{};
  74. size_t stage_index{};
  75. };
  76. class ComputeEnvironment final : public GenericEnvironment {
  77. public:
  78. explicit ComputeEnvironment() = default;
  79. explicit ComputeEnvironment(Tegra::Engines::KeplerCompute& kepler_compute_,
  80. Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  81. u32 start_address_);
  82. ~ComputeEnvironment() override = default;
  83. u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  84. Shader::TextureType ReadTextureType(u32 handle) override;
  85. private:
  86. Tegra::Engines::KeplerCompute* kepler_compute{};
  87. };
  88. class FileEnvironment final : public Shader::Environment {
  89. public:
  90. FileEnvironment() = default;
  91. ~FileEnvironment() override = default;
  92. FileEnvironment& operator=(FileEnvironment&&) noexcept = default;
  93. FileEnvironment(FileEnvironment&&) noexcept = default;
  94. FileEnvironment& operator=(const FileEnvironment&) = delete;
  95. FileEnvironment(const FileEnvironment&) = delete;
  96. void Deserialize(std::ifstream& file);
  97. [[nodiscard]] u64 ReadInstruction(u32 address) override;
  98. [[nodiscard]] u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  99. [[nodiscard]] Shader::TextureType ReadTextureType(u32 handle) override;
  100. [[nodiscard]] u32 LocalMemorySize() const override;
  101. [[nodiscard]] u32 SharedMemorySize() const override;
  102. [[nodiscard]] u32 TextureBoundBuffer() const override;
  103. [[nodiscard]] std::array<u32, 3> WorkgroupSize() const override;
  104. void Dump(u64 hash) 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. u32 initial_offset{};
  116. };
  117. void SerializePipeline(std::span<const char> key, std::span<const GenericEnvironment* const> envs,
  118. const std::filesystem::path& filename, u32 cache_version);
  119. template <typename Key, typename Envs>
  120. void SerializePipeline(const Key& key, const Envs& envs, const std::filesystem::path& filename,
  121. u32 cache_version) {
  122. static_assert(std::is_trivially_copyable_v<Key>);
  123. static_assert(std::has_unique_object_representations_v<Key>);
  124. SerializePipeline(std::span(reinterpret_cast<const char*>(&key), sizeof(key)),
  125. std::span(envs.data(), envs.size()), filename, cache_version);
  126. }
  127. void LoadPipelines(
  128. std::stop_token stop_loading, const std::filesystem::path& filename, u32 expected_cache_version,
  129. Common::UniqueFunction<void, std::ifstream&, FileEnvironment> load_compute,
  130. Common::UniqueFunction<void, std::ifstream&, std::vector<FileEnvironment>> load_graphics);
  131. } // namespace VideoCommon