registry.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2019 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 <optional>
  7. #include <type_traits>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include "common/common_types.h"
  11. #include "common/hash.h"
  12. #include "video_core/engines/const_buffer_engine_interface.h"
  13. #include "video_core/engines/maxwell_3d.h"
  14. #include "video_core/engines/shader_type.h"
  15. #include "video_core/guest_driver.h"
  16. namespace VideoCommon::Shader {
  17. struct SeparateSamplerKey {
  18. std::pair<u32, u32> buffers;
  19. std::pair<u32, u32> offsets;
  20. };
  21. } // namespace VideoCommon::Shader
  22. namespace std {
  23. template <>
  24. struct hash<VideoCommon::Shader::SeparateSamplerKey> {
  25. std::size_t operator()(const VideoCommon::Shader::SeparateSamplerKey& key) const noexcept {
  26. return std::hash<u32>{}(key.buffers.first ^ key.buffers.second ^ key.offsets.first ^
  27. key.offsets.second);
  28. }
  29. };
  30. template <>
  31. struct equal_to<VideoCommon::Shader::SeparateSamplerKey> {
  32. bool operator()(const VideoCommon::Shader::SeparateSamplerKey& lhs,
  33. const VideoCommon::Shader::SeparateSamplerKey& rhs) const noexcept {
  34. return lhs.buffers == rhs.buffers && lhs.offsets == rhs.offsets;
  35. }
  36. };
  37. } // namespace std
  38. namespace VideoCommon::Shader {
  39. using KeyMap = std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>;
  40. using BoundSamplerMap = std::unordered_map<u32, Tegra::Engines::SamplerDescriptor>;
  41. using SeparateSamplerMap =
  42. std::unordered_map<SeparateSamplerKey, Tegra::Engines::SamplerDescriptor>;
  43. using BindlessSamplerMap =
  44. std::unordered_map<std::pair<u32, u32>, Tegra::Engines::SamplerDescriptor, Common::PairHash>;
  45. struct GraphicsInfo {
  46. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  47. std::array<Maxwell::TransformFeedbackLayout, Maxwell::NumTransformFeedbackBuffers>
  48. tfb_layouts{};
  49. std::array<std::array<u8, 128>, Maxwell::NumTransformFeedbackBuffers> tfb_varying_locs{};
  50. Maxwell::PrimitiveTopology primitive_topology{};
  51. Maxwell::TessellationPrimitive tessellation_primitive{};
  52. Maxwell::TessellationSpacing tessellation_spacing{};
  53. bool tfb_enabled = false;
  54. bool tessellation_clockwise = false;
  55. };
  56. static_assert(std::is_trivially_copyable_v<GraphicsInfo> &&
  57. std::is_standard_layout_v<GraphicsInfo>);
  58. struct ComputeInfo {
  59. std::array<u32, 3> workgroup_size{};
  60. u32 shared_memory_size_in_words = 0;
  61. u32 local_memory_size_in_words = 0;
  62. };
  63. static_assert(std::is_trivially_copyable_v<ComputeInfo> && std::is_standard_layout_v<ComputeInfo>);
  64. struct SerializedRegistryInfo {
  65. VideoCore::GuestDriverProfile guest_driver_profile;
  66. u32 bound_buffer = 0;
  67. GraphicsInfo graphics;
  68. ComputeInfo compute;
  69. };
  70. /**
  71. * The Registry is a class use to interface the 3D and compute engines with the shader compiler.
  72. * With it, the shader can obtain required data from GPU state and store it for disk shader
  73. * compilation.
  74. */
  75. class Registry {
  76. public:
  77. explicit Registry(Tegra::Engines::ShaderType shader_stage, const SerializedRegistryInfo& info);
  78. explicit Registry(Tegra::Engines::ShaderType shader_stage,
  79. Tegra::Engines::ConstBufferEngineInterface& engine);
  80. ~Registry();
  81. /// Retrieves a key from the registry, if it's registered, it will give the registered value, if
  82. /// not it will obtain it from maxwell3d and register it.
  83. std::optional<u32> ObtainKey(u32 buffer, u32 offset);
  84. std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);
  85. std::optional<Tegra::Engines::SamplerDescriptor> ObtainSeparateSampler(
  86. std::pair<u32, u32> buffers, std::pair<u32, u32> offsets);
  87. std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);
  88. /// Inserts a key.
  89. void InsertKey(u32 buffer, u32 offset, u32 value);
  90. /// Inserts a bound sampler key.
  91. void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);
  92. /// Inserts a bindless sampler key.
  93. void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);
  94. /// Checks keys and samplers against engine's current const buffers.
  95. /// Returns true if they are the same value, false otherwise.
  96. bool IsConsistent() const;
  97. /// Returns true if the keys are equal to the other ones in the registry.
  98. bool HasEqualKeys(const Registry& rhs) const;
  99. /// Returns graphics information from this shader
  100. const GraphicsInfo& GetGraphicsInfo() const;
  101. /// Returns compute information from this shader
  102. const ComputeInfo& GetComputeInfo() const;
  103. /// Gives an getter to the const buffer keys in the database.
  104. const KeyMap& GetKeys() const {
  105. return keys;
  106. }
  107. /// Gets samplers database.
  108. const BoundSamplerMap& GetBoundSamplers() const {
  109. return bound_samplers;
  110. }
  111. /// Gets bindless samplers database.
  112. const BindlessSamplerMap& GetBindlessSamplers() const {
  113. return bindless_samplers;
  114. }
  115. /// Gets bound buffer used on this shader
  116. u32 GetBoundBuffer() const {
  117. return bound_buffer;
  118. }
  119. /// Obtains access to the guest driver's profile.
  120. VideoCore::GuestDriverProfile& AccessGuestDriverProfile() {
  121. return engine ? engine->AccessGuestDriverProfile() : stored_guest_driver_profile;
  122. }
  123. private:
  124. const Tegra::Engines::ShaderType stage;
  125. VideoCore::GuestDriverProfile stored_guest_driver_profile;
  126. Tegra::Engines::ConstBufferEngineInterface* engine = nullptr;
  127. KeyMap keys;
  128. BoundSamplerMap bound_samplers;
  129. SeparateSamplerMap separate_samplers;
  130. BindlessSamplerMap bindless_samplers;
  131. u32 bound_buffer;
  132. GraphicsInfo graphics_info;
  133. ComputeInfo compute_info;
  134. };
  135. } // namespace VideoCommon::Shader