2017-01-20 03:52:40 +00:00
|
|
|
#ifndef BOO_GRAPHICSDEV_COMMON_HPP
|
|
|
|
#define BOO_GRAPHICSDEV_COMMON_HPP
|
|
|
|
|
|
|
|
/* Private header for managing shader data
|
|
|
|
* binding lifetimes through rendering cycle */
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
#include <atomic>
|
2017-03-14 07:02:53 +00:00
|
|
|
#include <vector>
|
2017-01-20 03:52:40 +00:00
|
|
|
#include "boo/graphicsdev/IGraphicsDataFactory.hpp"
|
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
|
|
|
class IGraphicsDataPriv : public IGraphicsData
|
|
|
|
{
|
|
|
|
std::atomic_int m_refCount = {1};
|
|
|
|
public:
|
|
|
|
void increment() { m_refCount++; }
|
|
|
|
void decrement()
|
|
|
|
{
|
|
|
|
if (m_refCount.fetch_sub(1) == 1)
|
2017-03-14 07:02:53 +00:00
|
|
|
delete this;
|
2017-01-20 03:52:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class IShaderDataBindingPriv : public IShaderDataBinding
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
IGraphicsDataPriv* m_parent;
|
|
|
|
std::vector<IGraphicsDataPriv*> m_depDatas;
|
2017-01-20 03:52:40 +00:00
|
|
|
|
|
|
|
public:
|
2017-03-14 07:02:53 +00:00
|
|
|
IShaderDataBindingPriv(IGraphicsDataPriv* p) : m_parent(p) {}
|
2017-01-20 03:52:40 +00:00
|
|
|
class Token
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
IGraphicsDataPriv* m_data = nullptr;
|
2017-01-20 03:52:40 +00:00
|
|
|
public:
|
|
|
|
Token() = default;
|
|
|
|
Token(const IShaderDataBindingPriv* p)
|
2017-03-14 07:02:53 +00:00
|
|
|
: m_data(p->m_parent) { m_data->increment(); }
|
2017-01-20 03:52:40 +00:00
|
|
|
Token& operator=(const Token&) = delete;
|
|
|
|
Token(const Token&) = delete;
|
|
|
|
Token& operator=(Token&& other)
|
|
|
|
{ m_data = other.m_data; other.m_data = nullptr; return *this; }
|
|
|
|
Token(Token&& other)
|
|
|
|
{ m_data = other.m_data; other.m_data = nullptr; }
|
2017-03-14 07:02:53 +00:00
|
|
|
~Token() { if (m_data) { m_data->decrement(); } }
|
2017-01-20 03:52:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Token lock() const { return Token(this); }
|
2017-03-14 07:02:53 +00:00
|
|
|
~IShaderDataBindingPriv()
|
|
|
|
{
|
|
|
|
for (IGraphicsDataPriv* dep : m_depDatas)
|
|
|
|
dep->decrement();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void addDepData(IGraphicsData* data)
|
|
|
|
{
|
|
|
|
IGraphicsDataPriv* d = static_cast<IGraphicsDataPriv*>(data);
|
|
|
|
if (d != m_parent)
|
|
|
|
{
|
|
|
|
m_depDatas.push_back(d);
|
|
|
|
d->increment();
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 03:52:40 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
template <class FactoryImpl, class ShaderImpl>
|
|
|
|
class IShareableShader
|
|
|
|
{
|
|
|
|
std::atomic_int m_refCount = {0};
|
|
|
|
FactoryImpl& m_factory;
|
2017-03-05 22:59:58 +00:00
|
|
|
uint64_t m_srckey, m_binKey;
|
2017-03-05 07:54:58 +00:00
|
|
|
public:
|
2017-03-05 22:59:58 +00:00
|
|
|
IShareableShader(FactoryImpl& factory, uint64_t srcKey, uint64_t binKey)
|
|
|
|
: m_factory(factory), m_srckey(srcKey), m_binKey(binKey) {}
|
2017-03-05 07:54:58 +00:00
|
|
|
void increment() { m_refCount++; }
|
|
|
|
void decrement()
|
|
|
|
{
|
|
|
|
if (m_refCount.fetch_sub(1) == 1)
|
2017-03-05 22:59:58 +00:00
|
|
|
m_factory._unregisterShareableShader(m_srckey, m_binKey);
|
2017-03-05 07:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Token
|
|
|
|
{
|
|
|
|
IShareableShader<FactoryImpl, ShaderImpl>* m_parent = nullptr;
|
|
|
|
public:
|
|
|
|
Token() = default;
|
|
|
|
Token(IShareableShader* p)
|
|
|
|
: m_parent(p)
|
|
|
|
{ m_parent->increment(); }
|
|
|
|
Token& operator=(const Token&) = delete;
|
|
|
|
Token(const Token&) = delete;
|
|
|
|
Token& operator=(Token&& other)
|
|
|
|
{ m_parent = other.m_parent; other.m_parent = nullptr; return *this; }
|
|
|
|
Token(Token&& other)
|
|
|
|
{ m_parent = other.m_parent; other.m_parent = nullptr; }
|
|
|
|
~Token() { if (m_parent) m_parent->decrement(); }
|
|
|
|
operator bool() const { return m_parent != nullptr; }
|
|
|
|
ShaderImpl& get() const { return static_cast<ShaderImpl&>(*m_parent); }
|
|
|
|
};
|
|
|
|
|
|
|
|
Token lock() { return Token(this); }
|
|
|
|
};
|
|
|
|
|
2017-01-20 03:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BOO_GRAPHICSDEV_COMMON_HPP
|