瀏覽代碼

[REFACTOR] Add micro samplers skeleton

Panagiotis Christopoulos Charitos 8 年之前
父節點
當前提交
76385eabe5
共有 2 個文件被更改,包括 156 次插入0 次删除
  1. 46 0
      src/anki/gr/vulkan/SamplerFactory.cpp
  2. 110 0
      src/anki/gr/vulkan/SamplerFactory.h

+ 46 - 0
src/anki/gr/vulkan/SamplerFactory.cpp

@@ -0,0 +1,46 @@
+// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <anki/gr/vulkan/SamplerFactory.h>
+#include <anki/gr/vulkan/GrManagerImpl.h>
+
+namespace anki
+{
+
+MicroSampler::~MicroSampler()
+{
+	// TODO
+}
+
+GrAllocator<U8> MicroSampler::getAllocator() const
+{
+	return m_factory->m_gr->getAllocator();
+}
+
+Error MicroSampler::init(const SamplerInitInfo& inf)
+{
+	// TODO
+	return Error::NONE;
+}
+
+Error SamplerFactory::init(GrManagerImpl* gr)
+{
+	ANKI_ASSERT(gr);
+	// TODO
+	return Error::NONE;
+}
+
+void SamplerFactory::destroy()
+{
+	// TODO
+}
+
+Error SamplerFactory::newInstance(const SamplerInitInfo& inf, MicroSamplerPtr& psampler)
+{
+	// TODO
+	return Error::NONE;
+}
+
+} // end namespace anki

+ 110 - 0
src/anki/gr/vulkan/SamplerFactory.h

@@ -0,0 +1,110 @@
+// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include <anki/gr/vulkan/FenceFactory.h>
+#include <anki/gr/vulkan/MicroObjectRecycler.h>
+
+namespace anki
+{
+
+// Forward
+class SamplerFactory;
+
+/// @addtogroup vulkan
+/// @{
+
+/// A thin wapper over VkSampler.
+class MicroSampler
+{
+	friend class MicroSamplerPtrDeleter;
+	friend class SamplerFactory;
+	template<typename, typename>
+	friend class GenericPoolAllocator;
+
+public:
+	const VkSampler& getHandle() const
+	{
+		ANKI_ASSERT(m_handle);
+		return m_handle;
+	}
+
+	Atomic<U32>& getRefcount()
+	{
+		return m_refcount;
+	}
+
+	GrAllocator<U8> getAllocator() const;
+
+	MicroFencePtr& getFence()
+	{
+		return m_dummyFence;
+	}
+
+private:
+	VkSampler m_handle = VK_NULL_HANDLE;
+	Atomic<U32> m_refcount = {0};
+	SamplerFactory* m_factory = nullptr;
+
+	MicroFencePtr m_dummyFence; ///< This is a dummy fence to satisfy the interface.
+
+	MicroSampler(SamplerFactory* f)
+		: m_factory(f)
+	{
+		ANKI_ASSERT(f);
+	}
+
+	~MicroSampler();
+
+	ANKI_USE_RESULT Error init(const SamplerInitInfo& inf);
+};
+
+/// MicroSamplerPtr deleter.
+class MicroSamplerPtrDeleter
+{
+public:
+	void operator()(MicroSampler* s);
+};
+
+/// MicroSampler smart pointer.
+using MicroSamplerPtr = IntrusivePtr<MicroSampler, MicroSamplerPtrDeleter>;
+
+/// Sampler factory. Used to avoid creating too many duplicate samplers.
+class SamplerFactory
+{
+	friend class MicroSampler;
+	friend class MicroSamplerPtrDeleter;
+
+public:
+	SamplerFactory()
+	{
+	}
+
+	~SamplerFactory()
+	{
+		ANKI_ASSERT(m_gr == nullptr && "Forgot to call destroy()");
+	}
+
+	ANKI_USE_RESULT Error init(GrManagerImpl* gr);
+
+	void destroy();
+
+	/// Create a new sampler.
+	ANKI_USE_RESULT Error newInstance(const SamplerInitInfo& inf, MicroSamplerPtr& psampler);
+
+private:
+	GrManagerImpl* m_gr = nullptr;
+	MicroObjectRecycler<MicroSampler> m_recycler;
+};
+/// @}
+
+inline void MicroSamplerPtrDeleter::operator()(MicroSampler* s)
+{
+	ANKI_ASSERT(s);
+	s->m_factory->m_recycler.recycle(s);
+}
+
+} // end namespace anki