shader_environment.h 7.2 KB

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