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-11-03 09:39:26 +00:00
|
|
|
#include <mutex>
|
2017-01-20 03:52:40 +00:00
|
|
|
#include "boo/graphicsdev/IGraphicsDataFactory.hpp"
|
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
struct BaseGraphicsData;
|
|
|
|
struct BaseGraphicsPool;
|
|
|
|
|
|
|
|
template<class NodeCls, class DataCls = BaseGraphicsData>
|
|
|
|
struct GraphicsDataNode;
|
|
|
|
|
|
|
|
/** Inherited by data factory implementations to track the head data and pool nodes */
|
|
|
|
struct GraphicsDataFactoryHead
|
2017-01-20 03:52:40 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
std::mutex m_dataMutex;
|
|
|
|
BaseGraphicsData* m_dataHead = nullptr;
|
|
|
|
BaseGraphicsPool* m_poolHead = nullptr;
|
|
|
|
};
|
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
/** Linked-list iterator shareable by data container types */
|
|
|
|
template<class T>
|
|
|
|
class DataIterator
|
|
|
|
{
|
|
|
|
T* m_node;
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
|
|
|
using pointer = T*;
|
|
|
|
using reference = T&;
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
|
|
|
|
explicit DataIterator(T* node) : m_node(node) {}
|
|
|
|
T& operator*() const { return *m_node; }
|
|
|
|
bool operator!=(const DataIterator& other) const { return m_node != other.m_node; }
|
|
|
|
DataIterator& operator++() { m_node = m_node->m_next; return *this; }
|
|
|
|
DataIterator& operator--() { m_node = m_node->m_prev; return *this; }
|
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
/** Private generalized data container class.
|
|
|
|
* Keeps head pointers to all graphics objects by type
|
|
|
|
*/
|
|
|
|
struct BaseGraphicsData : IObj
|
|
|
|
{
|
|
|
|
GraphicsDataFactoryHead& m_head;
|
|
|
|
BaseGraphicsData* m_next;
|
|
|
|
BaseGraphicsData* m_prev = nullptr;
|
|
|
|
GraphicsDataNode<IShaderPipeline, BaseGraphicsData>* m_SPs = nullptr;
|
|
|
|
GraphicsDataNode<IShaderDataBinding, BaseGraphicsData>* m_SBinds = nullptr;
|
|
|
|
GraphicsDataNode<IGraphicsBufferS, BaseGraphicsData>* m_SBufs = nullptr;
|
|
|
|
GraphicsDataNode<IGraphicsBufferD, BaseGraphicsData>* m_DBufs = nullptr;
|
|
|
|
GraphicsDataNode<ITextureS, BaseGraphicsData>* m_STexs = nullptr;
|
|
|
|
GraphicsDataNode<ITextureSA, BaseGraphicsData>* m_SATexs = nullptr;
|
|
|
|
GraphicsDataNode<ITextureD, BaseGraphicsData>* m_DTexs = nullptr;
|
|
|
|
GraphicsDataNode<ITextureR, BaseGraphicsData>* m_RTexs = nullptr;
|
|
|
|
GraphicsDataNode<IVertexFormat, BaseGraphicsData>* m_VFmts = nullptr;
|
|
|
|
template<class T> GraphicsDataNode<T, BaseGraphicsData>*& getHead();
|
2017-11-06 06:53:54 +00:00
|
|
|
template<class T> size_t countForward()
|
|
|
|
{ auto* head = getHead<T>(); return head ? head->countForward() : 0; }
|
2017-11-03 09:39:26 +00:00
|
|
|
|
|
|
|
explicit BaseGraphicsData(GraphicsDataFactoryHead& head)
|
|
|
|
: m_head(head)
|
2017-01-20 03:52:40 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_head.m_dataMutex);
|
|
|
|
m_next = head.m_dataHead;
|
2017-11-05 06:12:49 +00:00
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = this;
|
2017-11-03 09:39:26 +00:00
|
|
|
head.m_dataHead = this;
|
2017-01-20 03:52:40 +00:00
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
~BaseGraphicsData()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_head.m_dataMutex);
|
|
|
|
if (m_prev)
|
|
|
|
{
|
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = m_prev;
|
|
|
|
m_prev->m_next = m_next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_next)
|
2017-11-05 06:12:49 +00:00
|
|
|
m_next->m_prev = nullptr;
|
2017-11-03 09:39:26 +00:00
|
|
|
m_head.m_dataHead = m_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
using iterator = DataIterator<BaseGraphicsData>;
|
2017-11-03 09:39:26 +00:00
|
|
|
iterator begin() { return iterator(this); }
|
|
|
|
iterator end() { return iterator(nullptr); }
|
2017-01-20 03:52:40 +00:00
|
|
|
};
|
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<IShaderPipeline, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<IShaderPipeline>() { return m_SPs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<IShaderDataBinding, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<IShaderDataBinding>() { return m_SBinds; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<IGraphicsBufferS, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<IGraphicsBufferS>() { return m_SBufs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<IGraphicsBufferD, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<IGraphicsBufferD>() { return m_DBufs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<ITextureS, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<ITextureS>() { return m_STexs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<ITextureSA, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<ITextureSA>() { return m_SATexs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<ITextureD, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<ITextureD>() { return m_DTexs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<ITextureR, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<ITextureR>() { return m_RTexs; }
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<IVertexFormat, BaseGraphicsData>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsData::getHead<IVertexFormat>() { return m_VFmts; }
|
|
|
|
|
|
|
|
/** Private generalized pool container class.
|
2017-11-05 06:12:49 +00:00
|
|
|
* Keeps head pointer to exactly one dynamic buffer while otherwise conforming to BaseGraphicsData
|
2017-11-03 09:39:26 +00:00
|
|
|
*/
|
|
|
|
struct BaseGraphicsPool : IObj
|
2017-01-20 03:52:40 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
GraphicsDataFactoryHead& m_head;
|
|
|
|
BaseGraphicsPool* m_next;
|
|
|
|
BaseGraphicsPool* m_prev = nullptr;
|
|
|
|
GraphicsDataNode<IGraphicsBufferD, BaseGraphicsPool>* m_DBufs = nullptr;
|
|
|
|
template<class T> GraphicsDataNode<T, BaseGraphicsPool>*& getHead();
|
2017-11-06 06:53:54 +00:00
|
|
|
template<class T> size_t countForward()
|
|
|
|
{ auto* head = getHead<T>(); return head ? head->countForward() : 0; }
|
2017-01-20 03:52:40 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
explicit BaseGraphicsPool(GraphicsDataFactoryHead& head)
|
|
|
|
: m_head(head)
|
2017-01-20 03:52:40 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_head.m_dataMutex);
|
|
|
|
m_next = head.m_poolHead;
|
2017-11-05 06:12:49 +00:00
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = this;
|
2017-11-03 09:39:26 +00:00
|
|
|
head.m_poolHead = this;
|
|
|
|
}
|
|
|
|
~BaseGraphicsPool()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_head.m_dataMutex);
|
|
|
|
if (m_prev)
|
|
|
|
{
|
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = m_prev;
|
|
|
|
m_prev->m_next = m_next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_next)
|
2017-11-05 06:12:49 +00:00
|
|
|
m_next->m_prev = nullptr;
|
2017-11-03 09:39:26 +00:00
|
|
|
m_head.m_poolHead = m_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
using iterator = DataIterator<BaseGraphicsPool>;
|
2017-11-03 09:39:26 +00:00
|
|
|
iterator begin() { return iterator(this); }
|
|
|
|
iterator end() { return iterator(nullptr); }
|
|
|
|
};
|
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
template <> inline GraphicsDataNode<IGraphicsBufferD, BaseGraphicsPool>*&
|
2017-11-03 09:39:26 +00:00
|
|
|
BaseGraphicsPool::getHead<IGraphicsBufferD>() { return m_DBufs; }
|
|
|
|
|
|
|
|
/** Private generalised graphics object node.
|
|
|
|
* Keeps a strong reference to the data pool that it's a member of;
|
|
|
|
* as well as doubly-linked pointers to same-type sibling objects
|
|
|
|
*/
|
|
|
|
template<class NodeCls, class DataCls>
|
|
|
|
struct GraphicsDataNode : NodeCls
|
|
|
|
{
|
|
|
|
ObjToken<DataCls> m_data;
|
|
|
|
GraphicsDataNode<NodeCls, DataCls>* m_next;
|
|
|
|
GraphicsDataNode<NodeCls, DataCls>* m_prev = nullptr;
|
|
|
|
|
|
|
|
explicit GraphicsDataNode(const ObjToken<DataCls>& data)
|
|
|
|
: m_data(data)
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_data->m_head.m_dataMutex);
|
|
|
|
m_next = data->template getHead<NodeCls>();
|
2017-11-05 06:12:49 +00:00
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = this;
|
2017-11-03 09:39:26 +00:00
|
|
|
data->template getHead<NodeCls>() = this;
|
2017-03-14 07:02:53 +00:00
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
~GraphicsDataNode()
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_data->m_head.m_dataMutex);
|
|
|
|
if (m_prev)
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = m_prev;
|
|
|
|
m_prev->m_next = m_next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_next)
|
2017-11-05 06:12:49 +00:00
|
|
|
m_next->m_prev = nullptr;
|
2017-11-03 09:39:26 +00:00
|
|
|
m_data->template getHead<NodeCls>() = m_next;
|
2017-03-14 07:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
|
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
GraphicsDataNode<NodeCls, DataCls>* m_node;
|
|
|
|
public:
|
|
|
|
using value_type = NodeCls;
|
|
|
|
using pointer = NodeCls*;
|
|
|
|
using reference = NodeCls&;
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
|
|
|
|
explicit iterator(GraphicsDataNode<NodeCls, DataCls>* node) : m_node(node) {}
|
|
|
|
NodeCls& operator*() const { return *m_node; }
|
|
|
|
bool operator!=(const iterator& other) const { return m_node != other.m_node; }
|
|
|
|
iterator& operator++() { m_node = m_node->m_next; return *this; }
|
|
|
|
iterator& operator--() { m_node = m_node->m_prev; return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator begin() { return iterator(this); }
|
|
|
|
iterator end() { return iterator(nullptr); }
|
2017-11-06 06:53:54 +00:00
|
|
|
|
|
|
|
size_t countForward()
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
for (auto& n : *this)
|
|
|
|
++ret;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-20 03:52:40 +00:00
|
|
|
};
|
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
/** Hash table entry for owning sharable shader objects */
|
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; }
|
2017-11-02 09:24:50 +00:00
|
|
|
void reset() { if (m_parent) m_parent->decrement(); m_parent = nullptr; }
|
2017-03-05 07:54:58 +00:00
|
|
|
~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
|