const_buffer_locker.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. namespace VideoCommon::Shader {
  10. using KeyMap = std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>;
  11. using BoundSamplerMap = std::unordered_map<u32, Tegra::Engines::SamplerDescriptor>;
  12. using BindlessSamplerMap =
  13. std::unordered_map<std::pair<u32, u32>, Tegra::Engines::SamplerDescriptor, Common::PairHash>;
  14. class ConstBufferLocker {
  15. public:
  16. explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage);
  17. explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
  18. Tegra::Engines::ConstBufferEngineInterface* engine);
  19. // Checks if an engine is setup, it may be possible that during disk shader
  20. // cache run, the engines have not been created yet.
  21. bool IsEngineSet() const;
  22. // Use this to set/change the engine used for this shader.
  23. void SetEngine(Tegra::Engines::ConstBufferEngineInterface* engine);
  24. // Retrieves a key from the locker, if it's registered, it will give the
  25. // registered value, if not it will obtain it from maxwell3d and register it.
  26. std::optional<u32> ObtainKey(u32 buffer, u32 offset);
  27. std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);
  28. std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);
  29. // Manually inserts a key.
  30. void InsertKey(u32 buffer, u32 offset, u32 value);
  31. void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);
  32. void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);
  33. // Retrieves the number of keys registered.
  34. std::size_t NumKeys() const {
  35. if (!keys) {
  36. return 0;
  37. }
  38. return keys->size();
  39. }
  40. std::size_t NumBoundSamplers() const {
  41. if (!bound_samplers) {
  42. return 0;
  43. }
  44. return bound_samplers->size();
  45. }
  46. std::size_t NumBindlessSamplers() const {
  47. if (!bindless_samplers) {
  48. return 0;
  49. }
  50. return bindless_samplers->size();
  51. }
  52. // Gives an accessor to the key's database.
  53. // Pre: NumKeys > 0
  54. const KeyMap& AccessKeys() const {
  55. return *keys;
  56. }
  57. // Gives an accessor to the sampler's database.
  58. // Pre: NumBindlessSamplers > 0
  59. const BoundSamplerMap& AccessBoundSamplers() const {
  60. return *bound_samplers;
  61. }
  62. // Gives an accessor to the sampler's database.
  63. // Pre: NumBindlessSamplers > 0
  64. const BindlessSamplerMap& AccessBindlessSamplers() const {
  65. return *bindless_samplers;
  66. }
  67. // Checks keys & samplers against engine's current const buffers. Returns true if they
  68. // are the same value, false otherwise;
  69. bool IsConsistent() const;
  70. private:
  71. Tegra::Engines::ConstBufferEngineInterface* engine;
  72. Tegra::Engines::ShaderType shader_stage;
  73. // All containers are lazy initialized as most shaders don't use them.
  74. std::shared_ptr<KeyMap> keys{};
  75. std::shared_ptr<BoundSamplerMap> bound_samplers{};
  76. std::shared_ptr<BindlessSamplerMap> bindless_samplers{};
  77. };
  78. } // namespace VideoCommon::Shader