shader_environment.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <type_traits>
  12. #include <unordered_map>
  13. #include <vector>
  14. #include "common/common_types.h"
  15. #include "common/polyfill_thread.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. bool HasHLEMacroState() const override {
  43. return has_hle_engine_state;
  44. }
  45. protected:
  46. std::optional<u64> TryFindSize();
  47. Tegra::Texture::TICEntry ReadTextureInfo(GPUVAddr tic_addr, u32 tic_limit,
  48. bool via_header_index, u32 raw);
  49. Tegra::MemoryManager* gpu_memory{};
  50. GPUVAddr program_base{};
  51. std::vector<u64> code;
  52. std::unordered_map<u32, Shader::TextureType> texture_types;
  53. std::unordered_map<u32, Shader::TexturePixelFormat> texture_pixel_formats;
  54. std::unordered_map<u64, u32> cbuf_values;
  55. std::unordered_map<u64, Shader::ReplaceConstant> cbuf_replacements;
  56. u32 local_memory_size{};
  57. u32 texture_bound{};
  58. u32 shared_memory_size{};
  59. std::array<u32, 3> workgroup_size{};
  60. u32 read_lowest = std::numeric_limits<u32>::max();
  61. u32 read_highest = 0;
  62. u32 cached_lowest = std::numeric_limits<u32>::max();
  63. u32 cached_highest = 0;
  64. u32 initial_offset = 0;
  65. u32 viewport_transform_state = 1;
  66. bool has_unbound_instructions = false;
  67. bool has_hle_engine_state = false;
  68. };
  69. class GraphicsEnvironment final : public GenericEnvironment {
  70. public:
  71. explicit GraphicsEnvironment() = default;
  72. explicit GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
  73. Tegra::MemoryManager& gpu_memory_,
  74. Tegra::Engines::Maxwell3D::Regs::ShaderType program,
  75. GPUVAddr program_base_, u32 start_address_);
  76. ~GraphicsEnvironment() override = default;
  77. u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  78. Shader::TextureType ReadTextureType(u32 handle) override;
  79. Shader::TexturePixelFormat ReadTexturePixelFormat(u32 handle) override;
  80. u32 ReadViewportTransformState() override;
  81. std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(u32 bank, u32 offset) override;
  82. private:
  83. Tegra::Engines::Maxwell3D* maxwell3d{};
  84. size_t stage_index{};
  85. };
  86. class ComputeEnvironment final : public GenericEnvironment {
  87. public:
  88. explicit ComputeEnvironment() = default;
  89. explicit ComputeEnvironment(Tegra::Engines::KeplerCompute& kepler_compute_,
  90. Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  91. u32 start_address_);
  92. ~ComputeEnvironment() override = default;
  93. u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  94. Shader::TextureType ReadTextureType(u32 handle) override;
  95. Shader::TexturePixelFormat ReadTexturePixelFormat(u32 handle) override;
  96. u32 ReadViewportTransformState() override;
  97. std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(
  98. [[maybe_unused]] u32 bank, [[maybe_unused]] u32 offset) override {
  99. return std::nullopt;
  100. }
  101. private:
  102. Tegra::Engines::KeplerCompute* kepler_compute{};
  103. };
  104. class FileEnvironment final : public Shader::Environment {
  105. public:
  106. FileEnvironment() = default;
  107. ~FileEnvironment() override = default;
  108. FileEnvironment& operator=(FileEnvironment&&) noexcept = default;
  109. FileEnvironment(FileEnvironment&&) noexcept = default;
  110. FileEnvironment& operator=(const FileEnvironment&) = delete;
  111. FileEnvironment(const FileEnvironment&) = delete;
  112. void Deserialize(std::ifstream& file);
  113. [[nodiscard]] u64 ReadInstruction(u32 address) override;
  114. [[nodiscard]] u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
  115. [[nodiscard]] Shader::TextureType ReadTextureType(u32 handle) override;
  116. [[nodiscard]] Shader::TexturePixelFormat ReadTexturePixelFormat(u32 handle) override;
  117. [[nodiscard]] u32 ReadViewportTransformState() override;
  118. [[nodiscard]] u32 LocalMemorySize() const override;
  119. [[nodiscard]] u32 SharedMemorySize() const override;
  120. [[nodiscard]] u32 TextureBoundBuffer() const override;
  121. [[nodiscard]] std::array<u32, 3> WorkgroupSize() const override;
  122. [[nodiscard]] std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(u32 bank,
  123. u32 offset) override;
  124. [[nodiscard]] bool HasHLEMacroState() const override {
  125. return cbuf_replacements.size() != 0;
  126. }
  127. void Dump(u64 hash) override;
  128. private:
  129. std::unique_ptr<u64[]> code;
  130. std::unordered_map<u32, Shader::TextureType> texture_types;
  131. std::unordered_map<u32, Shader::TexturePixelFormat> texture_pixel_formats;
  132. std::unordered_map<u64, u32> cbuf_values;
  133. std::unordered_map<u64, Shader::ReplaceConstant> cbuf_replacements;
  134. std::array<u32, 3> workgroup_size{};
  135. u32 local_memory_size{};
  136. u32 shared_memory_size{};
  137. u32 texture_bound{};
  138. u32 read_lowest{};
  139. u32 read_highest{};
  140. u32 initial_offset{};
  141. u32 viewport_transform_state = 1;
  142. };
  143. void SerializePipeline(std::span<const char> key, std::span<const GenericEnvironment* const> envs,
  144. const std::filesystem::path& filename, u32 cache_version);
  145. template <typename Key, typename Envs>
  146. void SerializePipeline(const Key& key, const Envs& envs, const std::filesystem::path& filename,
  147. u32 cache_version) {
  148. static_assert(std::is_trivially_copyable_v<Key>);
  149. static_assert(std::has_unique_object_representations_v<Key>);
  150. SerializePipeline(std::span(reinterpret_cast<const char*>(&key), sizeof(key)),
  151. std::span(envs.data(), envs.size()), filename, cache_version);
  152. }
  153. void LoadPipelines(
  154. std::stop_token stop_loading, const std::filesystem::path& filename, u32 expected_cache_version,
  155. Common::UniqueFunction<void, std::ifstream&, FileEnvironment> load_compute,
  156. Common::UniqueFunction<void, std::ifstream&, std::vector<FileEnvironment>> load_graphics);
  157. } // namespace VideoCommon