SamplerFactory.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2009-2021, 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/Vulkan/FenceFactory.h>
  7. #include <AnKi/Util/HashMap.h>
  8. namespace anki
  9. {
  10. // Forward
  11. class SamplerFactory;
  12. /// @addtogroup vulkan
  13. /// @{
  14. /// A thin wapper over VkSampler.
  15. class MicroSampler
  16. {
  17. friend class MicroSamplerPtrDeleter;
  18. friend class SamplerFactory;
  19. template<typename, typename>
  20. friend class GenericPoolAllocator;
  21. public:
  22. const VkSampler& getHandle() const
  23. {
  24. ANKI_ASSERT(m_handle);
  25. return m_handle;
  26. }
  27. Atomic<U32>& getRefcount()
  28. {
  29. return m_refcount;
  30. }
  31. private:
  32. VkSampler m_handle = VK_NULL_HANDLE;
  33. Atomic<U32> m_refcount = {0};
  34. SamplerFactory* m_factory = nullptr;
  35. MicroSampler(SamplerFactory* f)
  36. : m_factory(f)
  37. {
  38. ANKI_ASSERT(f);
  39. }
  40. ~MicroSampler();
  41. ANKI_USE_RESULT Error init(const SamplerInitInfo& inf);
  42. };
  43. /// MicroSamplerPtr deleter.
  44. class MicroSamplerPtrDeleter
  45. {
  46. public:
  47. void operator()(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
  57. {
  58. friend class MicroSampler;
  59. public:
  60. SamplerFactory()
  61. {
  62. }
  63. ~SamplerFactory()
  64. {
  65. ANKI_ASSERT(m_gr == nullptr && "Forgot to call destroy()");
  66. }
  67. void init(GrManagerImpl* gr);
  68. void destroy();
  69. /// Create a new sampler. It's thread-safe.
  70. ANKI_USE_RESULT Error newInstance(const SamplerInitInfo& inf, MicroSamplerPtr& psampler);
  71. private:
  72. GrManagerImpl* m_gr = nullptr;
  73. HashMap<U64, MicroSampler*> m_map;
  74. Mutex m_mtx;
  75. };
  76. /// @}
  77. } // end namespace anki