VkSampler.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/Sampler.h>
  7. #include <AnKi/Gr/Vulkan/VkCommon.h>
  8. #include <AnKi/Util/HashMap.h>
  9. namespace anki {
  10. /// @addtogroup vulkan
  11. /// @{
  12. /// A thin wapper over VkSampler.
  13. class MicroSampler
  14. {
  15. friend class MicroSamplerPtrDeleter;
  16. friend class SamplerFactory;
  17. ANKI_FRIEND_CALL_CONSTRUCTOR_AND_DESTRUCTOR
  18. public:
  19. const VkSampler& getHandle() const
  20. {
  21. ANKI_ASSERT(m_handle);
  22. return m_handle;
  23. }
  24. void retain() const
  25. {
  26. m_refcount.fetchAdd(1);
  27. }
  28. I32 release() const
  29. {
  30. return m_refcount.fetchSub(1);
  31. }
  32. I32 getRefcount() const
  33. {
  34. return m_refcount.load();
  35. }
  36. private:
  37. VkSampler m_handle = VK_NULL_HANDLE;
  38. mutable Atomic<I32> m_refcount = {0};
  39. MicroSampler() = default;
  40. ~MicroSampler();
  41. Error init(const SamplerInitInfo& inf);
  42. };
  43. /// MicroSamplerPtr deleter.
  44. class MicroSamplerPtrDeleter
  45. {
  46. public:
  47. void operator()([[maybe_unused]] MicroSampler* s)
  48. {
  49. ANKI_ASSERT(s);
  50. // Do nothing. The samplers will be destroyed at app shutdown
  51. }
  52. };
  53. /// MicroSampler smart pointer.
  54. using MicroSamplerPtr = IntrusivePtr<MicroSampler, MicroSamplerPtrDeleter>;
  55. /// Sampler factory. Used to avoid creating too many duplicate samplers.
  56. class SamplerFactory : public MakeSingleton<SamplerFactory>
  57. {
  58. friend class MicroSampler;
  59. public:
  60. ~SamplerFactory()
  61. {
  62. destroy();
  63. }
  64. /// Create a new sampler. It's thread-safe.
  65. Error newInstance(const SamplerInitInfo& inf, MicroSamplerPtr& psampler);
  66. private:
  67. GrHashMap<U64, MicroSampler*> m_map;
  68. Mutex m_mtx;
  69. void destroy();
  70. };
  71. /// Vulkan implementation of Sampler.
  72. class SamplerImpl final : public Sampler
  73. {
  74. public:
  75. MicroSamplerPtr m_sampler;
  76. SamplerImpl(CString name)
  77. : Sampler(name)
  78. {
  79. }
  80. ~SamplerImpl()
  81. {
  82. }
  83. };
  84. /// @}
  85. } // end namespace anki