const_buffer_locker.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <optional>
  6. #include <unordered_map>
  7. #include "common/common_types.h"
  8. #include "common/hash.h"
  9. #include "video_core/engines/const_buffer_engine_interface.h"
  10. #include "video_core/engines/shader_type.h"
  11. #include "video_core/guest_driver.h"
  12. namespace VideoCommon::Shader {
  13. using KeyMap = std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>;
  14. using BoundSamplerMap = std::unordered_map<u32, Tegra::Engines::SamplerDescriptor>;
  15. using BindlessSamplerMap =
  16. std::unordered_map<std::pair<u32, u32>, Tegra::Engines::SamplerDescriptor, Common::PairHash>;
  17. /**
  18. * The ConstBufferLocker is a class use to interface the 3D and compute engines with the shader
  19. * compiler. with it, the shader can obtain required data from GPU state and store it for disk
  20. * shader compilation.
  21. */
  22. class ConstBufferLocker {
  23. public:
  24. explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage);
  25. explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
  26. Tegra::Engines::ConstBufferEngineInterface& engine);
  27. ~ConstBufferLocker();
  28. /// Retrieves a key from the locker, if it's registered, it will give the registered value, if
  29. /// not it will obtain it from maxwell3d and register it.
  30. std::optional<u32> ObtainKey(u32 buffer, u32 offset);
  31. std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);
  32. std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);
  33. std::optional<u32> ObtainBoundBuffer();
  34. /// Inserts a key.
  35. void InsertKey(u32 buffer, u32 offset, u32 value);
  36. /// Inserts a bound sampler key.
  37. void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);
  38. /// Inserts a bindless sampler key.
  39. void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);
  40. /// Set the bound buffer for this locker.
  41. void SetBoundBuffer(u32 buffer);
  42. /// Checks keys and samplers against engine's current const buffers. Returns true if they are
  43. /// the same value, false otherwise;
  44. bool IsConsistent() const;
  45. /// Returns true if the keys are equal to the other ones in the locker.
  46. bool HasEqualKeys(const ConstBufferLocker& rhs) const;
  47. /// Gives an getter to the const buffer keys in the database.
  48. const KeyMap& GetKeys() const {
  49. return keys;
  50. }
  51. /// Gets samplers database.
  52. const BoundSamplerMap& GetBoundSamplers() const {
  53. return bound_samplers;
  54. }
  55. /// Gets bindless samplers database.
  56. const BindlessSamplerMap& GetBindlessSamplers() const {
  57. return bindless_samplers;
  58. }
  59. u32 GetBoundBuffer() const {
  60. return bound_buffer;
  61. }
  62. VideoCore::GuestDriverProfile* AccessGuestDriverProfile() {
  63. if (engine) {
  64. return &(engine->AccessGuestDriverProfile());
  65. }
  66. return nullptr;
  67. }
  68. private:
  69. const Tegra::Engines::ShaderType stage;
  70. Tegra::Engines::ConstBufferEngineInterface* engine = nullptr;
  71. KeyMap keys;
  72. BoundSamplerMap bound_samplers;
  73. BindlessSamplerMap bindless_samplers;
  74. bool bound_buffer_saved{};
  75. u32 bound_buffer{};
  76. };
  77. } // namespace VideoCommon::Shader