boo/lib/graphicsdev/Common.hpp

165 lines
6.8 KiB
C++
Raw Normal View History

2018-10-07 03:36:44 +00:00
#pragma once
/* Private header for managing shader data
* binding lifetimes through rendering cycle */
2017-01-21 00:19:18 +00:00
#include <atomic>
#include <vector>
#include <mutex>
#include <cassert>
#include "boo/graphicsdev/IGraphicsDataFactory.hpp"
#include "../Common.hpp"
namespace boo
{
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-11-08 07:33:10 +00:00
std::recursive_mutex m_dataMutex;
BaseGraphicsData* m_dataHead = nullptr;
BaseGraphicsPool* m_poolHead = nullptr;
~GraphicsDataFactoryHead()
{
assert(m_dataHead == nullptr && "Dangling graphics data pools detected");
assert(m_poolHead == nullptr && "Dangling graphics data pools detected");
}
};
/** Private generalized data container class.
* Keeps head pointers to all graphics objects by type
*/
struct BaseGraphicsData : ListNode<BaseGraphicsData, GraphicsDataFactoryHead*>
{
static BaseGraphicsData*& _getHeadPtr(GraphicsDataFactoryHead* head) { return head->m_dataHead; }
static std::unique_lock<std::recursive_mutex> _getHeadLock(GraphicsDataFactoryHead* head)
{ return std::unique_lock<std::recursive_mutex>{head->m_dataMutex}; }
2018-05-20 06:11:49 +00:00
__BooTraceFields
2018-10-07 02:49:22 +00:00
GraphicsDataNode<IShaderStage, BaseGraphicsData>* m_Ss = 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;
template<class T> GraphicsDataNode<T, BaseGraphicsData>*& getHead();
template<class T> size_t countForward()
{ auto* head = getHead<T>(); return head ? head->countForward() : 0; }
2017-11-09 08:10:43 +00:00
std::unique_lock<std::recursive_mutex> destructorLock() override
{ return std::unique_lock<std::recursive_mutex>{m_head->m_dataMutex}; }
2018-05-20 06:11:49 +00:00
explicit BaseGraphicsData(GraphicsDataFactoryHead& head __BooTraceArgs)
: ListNode<BaseGraphicsData, GraphicsDataFactoryHead*>(&head) __BooTraceInitializer
{}
};
2018-10-07 02:49:22 +00:00
template <> inline GraphicsDataNode<IShaderStage, BaseGraphicsData>*&
BaseGraphicsData::getHead<IShaderStage>() { return m_Ss; }
template <> inline GraphicsDataNode<IShaderPipeline, BaseGraphicsData>*&
BaseGraphicsData::getHead<IShaderPipeline>() { return m_SPs; }
template <> inline GraphicsDataNode<IShaderDataBinding, BaseGraphicsData>*&
BaseGraphicsData::getHead<IShaderDataBinding>() { return m_SBinds; }
template <> inline GraphicsDataNode<IGraphicsBufferS, BaseGraphicsData>*&
BaseGraphicsData::getHead<IGraphicsBufferS>() { return m_SBufs; }
template <> inline GraphicsDataNode<IGraphicsBufferD, BaseGraphicsData>*&
BaseGraphicsData::getHead<IGraphicsBufferD>() { return m_DBufs; }
template <> inline GraphicsDataNode<ITextureS, BaseGraphicsData>*&
BaseGraphicsData::getHead<ITextureS>() { return m_STexs; }
template <> inline GraphicsDataNode<ITextureSA, BaseGraphicsData>*&
BaseGraphicsData::getHead<ITextureSA>() { return m_SATexs; }
template <> inline GraphicsDataNode<ITextureD, BaseGraphicsData>*&
BaseGraphicsData::getHead<ITextureD>() { return m_DTexs; }
template <> inline GraphicsDataNode<ITextureR, BaseGraphicsData>*&
BaseGraphicsData::getHead<ITextureR>() { return m_RTexs; }
/** Private generalized pool container class.
* Keeps head pointer to exactly one dynamic buffer while otherwise conforming to BaseGraphicsData
*/
struct BaseGraphicsPool : ListNode<BaseGraphicsPool, GraphicsDataFactoryHead*>
{
static BaseGraphicsPool*& _getHeadPtr(GraphicsDataFactoryHead* head) { return head->m_poolHead; }
static std::unique_lock<std::recursive_mutex> _getHeadLock(GraphicsDataFactoryHead* head)
{ return std::unique_lock<std::recursive_mutex>{head->m_dataMutex}; }
2018-05-20 06:11:49 +00:00
__BooTraceFields
GraphicsDataNode<IGraphicsBufferD, BaseGraphicsPool>* m_DBufs = nullptr;
template<class T> GraphicsDataNode<T, BaseGraphicsPool>*& getHead();
template<class T> size_t countForward()
{ auto* head = getHead<T>(); return head ? head->countForward() : 0; }
2017-11-09 08:10:43 +00:00
std::unique_lock<std::recursive_mutex> destructorLock() override
{ return std::unique_lock<std::recursive_mutex>{m_head->m_dataMutex}; }
2018-05-20 06:11:49 +00:00
explicit BaseGraphicsPool(GraphicsDataFactoryHead& head __BooTraceArgs)
: ListNode<BaseGraphicsPool, GraphicsDataFactoryHead*>(&head) __BooTraceInitializer
{}
};
template <> inline GraphicsDataNode<IGraphicsBufferD, BaseGraphicsPool>*&
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 : ListNode<GraphicsDataNode<NodeCls, DataCls>, ObjToken<DataCls>, NodeCls>
{
using base = ListNode<GraphicsDataNode<NodeCls, DataCls>, ObjToken<DataCls>, NodeCls>;
static GraphicsDataNode<NodeCls, DataCls>*& _getHeadPtr(ObjToken<DataCls>& head)
{ return head->template getHead<NodeCls>(); }
static std::unique_lock<std::recursive_mutex> _getHeadLock(ObjToken<DataCls>& head)
{ return std::unique_lock<std::recursive_mutex>{head->m_head->m_dataMutex}; }
2017-11-09 08:10:43 +00:00
std::unique_lock<std::recursive_mutex> destructorLock() override
{ return std::unique_lock<std::recursive_mutex>{base::m_head->m_head->m_dataMutex}; }
explicit GraphicsDataNode(const ObjToken<DataCls>& data)
: ListNode<GraphicsDataNode<NodeCls, DataCls>, ObjToken<DataCls>, NodeCls>(data)
{}
2017-12-07 04:08:44 +00:00
class iterator
{
GraphicsDataNode<NodeCls, DataCls>* m_node;
public:
2017-12-07 04:08:44 +00:00
using iterator_category = std::bidirectional_iterator_tag;
using value_type = NodeCls;
using difference_type = std::ptrdiff_t;
using pointer = NodeCls*;
using reference = NodeCls&;
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); }
size_t countForward()
{
size_t ret = 0;
for (auto& n : *this)
++ret;
return ret;
}
};
2018-01-20 03:02:29 +00:00
void UpdateGammaLUT(ITextureD* tex, float gamma);
}