const_buffer_locker.h 2.8 KB

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