registry.h 4.6 KB

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