SamplerFactory.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Forward
  10. class SamplerFactory;
  11. /// @addtogroup vulkan
  12. /// @{
  13. /// A thin wapper over VkSampler.
  14. class MicroSampler
  15. {
  16. friend class MicroSamplerPtrDeleter;
  17. friend class SamplerFactory;
  18. template<typename, typename>
  19. friend class GenericPoolAllocator;
  20. public:
  21. const VkSampler& getHandle() const
  22. {
  23. ANKI_ASSERT(m_handle);
  24. return m_handle;
  25. }
  26. Atomic<U32>& getRefcount()
  27. {
  28. return m_refcount;
  29. }
  30. private:
  31. VkSampler m_handle = VK_NULL_HANDLE;
  32. Atomic<U32> m_refcount = {0};
  33. SamplerFactory* m_factory = nullptr;
  34. MicroSampler(SamplerFactory* f)
  35. : m_factory(f)
  36. {
  37. ANKI_ASSERT(f);
  38. }
  39. ~MicroSampler();
  40. ANKI_USE_RESULT Error init(const SamplerInitInfo& inf);
  41. };
  42. /// MicroSamplerPtr deleter.
  43. class MicroSamplerPtrDeleter
  44. {
  45. public:
  46. void operator()(MicroSampler* s)
  47. {
  48. ANKI_ASSERT(s);
  49. // Do nothing. The samplers will be destroyed at app shutdown
  50. }
  51. };
  52. /// MicroSampler smart pointer.
  53. using MicroSamplerPtr = IntrusivePtr<MicroSampler, MicroSamplerPtrDeleter>;
  54. /// Sampler factory. Used to avoid creating too many duplicate samplers.
  55. class SamplerFactory
  56. {
  57. friend class MicroSampler;
  58. public:
  59. SamplerFactory()
  60. {
  61. }
  62. ~SamplerFactory()
  63. {
  64. ANKI_ASSERT(m_gr == nullptr && "Forgot to call destroy()");
  65. }
  66. void init(GrManagerImpl* gr);
  67. void destroy();
  68. /// Create a new sampler. It's thread-safe.
  69. ANKI_USE_RESULT Error newInstance(const SamplerInitInfo& inf, MicroSamplerPtr& psampler);
  70. private:
  71. GrManagerImpl* m_gr = nullptr;
  72. HashMap<U64, MicroSampler*> m_map;
  73. Mutex m_mtx;
  74. };
  75. /// @}
  76. } // end namespace anki