Nebula
Loading...
Searching...
No Matches
graphicscontext.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18//------------------------------------------------------------------------------
19#include "core/refcounted.h"
20#include "timing/time.h"
21#include "ids/id.h"
22#include "ids/idallocator.h" // include this here since it will be used by all contexts
24#include "util/stringatom.h"
25#include "util/arraystack.h"
26#include "graphics/view.h"
27#include "graphicsentity.h"
28#include "coregraphics/window.h"
29
30#define __DeclarePluginContext() \
31private:\
32 static Graphics::GraphicsContextState __state;\
33 static Graphics::GraphicsContextFunctionBundle __bundle;\
34public:\
35 static void RegisterEntity(const Graphics::GraphicsEntityId id);\
36 static void DeregisterEntity(const Graphics::GraphicsEntityId id);\
37 static void DeregisterEntityImmediate(const Graphics::GraphicsEntityId id);\
38 static bool IsEntityRegistered(const Graphics::GraphicsEntityId id);\
39 static void Destroy(); \
40 static Graphics::ContextEntityId GetContextId(const Graphics::GraphicsEntityId id); \
41 static const Graphics::ContextEntityId& GetContextIdRef(const Graphics::GraphicsEntityId id); \
42 static void BeginBulkRegister(); \
43 static void EndBulkRegister(); \
44private:
45
46#define __DeclareContext() \
47 __DeclarePluginContext(); \
48 static void Defragment();
49
50
51#define __ImplementPluginContext(ctx) \
52Graphics::GraphicsContextState ctx::__state; \
53Graphics::GraphicsContextFunctionBundle ctx::__bundle; \
54void ctx::RegisterEntity(const Graphics::GraphicsEntityId id) \
55{\
56 Graphics::GraphicsContext::InternalRegisterEntity(id, std::forward<Graphics::GraphicsContextState>(__state));\
57}\
58\
59void ctx::DeregisterEntity(const Graphics::GraphicsEntityId id)\
60{\
61 Graphics::GraphicsContext::InternalDeregisterEntity(id, std::forward<Graphics::GraphicsContextState>(__state));\
62}\
63\
64void ctx::DeregisterEntityImmediate(const Graphics::GraphicsEntityId id)\
65{\
66 IndexT index = __state.entitySliceMap.FindIndex(id);\
67 n_assert(index != InvalidIndex);\
68 auto cid = __state.entitySliceMap.ValueAtIndex(id.id, index);\
69 __state.Dealloc(cid);\
70 __state.entitySliceMap.EraseIndex(id, index);\
71 if (__state.Defragment != nullptr)\
72 __state.Defragment();\
73}\
74\
75bool ctx::IsEntityRegistered(const Graphics::GraphicsEntityId id)\
76{\
77 return __state.entitySliceMap.Contains(id);\
78}\
79void ctx::Destroy()\
80{\
81 Graphics::GraphicsServer::Instance()->UnregisterGraphicsContext(&__bundle);\
82}\
83Graphics::ContextEntityId ctx::GetContextId(const Graphics::GraphicsEntityId id)\
84{\
85 IndexT idx = __state.entitySliceMap.FindIndex(id); \
86 if (idx == InvalidIndex) return Graphics::InvalidContextEntityId; \
87 else return __state.entitySliceMap.ValueAtIndex(id, idx); \
88}\
89const Graphics::ContextEntityId& ctx::GetContextIdRef(const Graphics::GraphicsEntityId id)\
90{\
91 IndexT idx = __state.entitySliceMap.FindIndex(id); \
92 n_assert(idx != InvalidIndex); \
93 return __state.entitySliceMap.ValueAtIndex(id, idx); \
94}\
95void ctx::BeginBulkRegister()\
96{\
97 __state.entitySliceMap.BeginBulkAdd();\
98}\
99void ctx::EndBulkRegister()\
100{\
101 __state.entitySliceMap.EndBulkAdd();\
102}
103
104#define __ImplementContext(ctx, idAllocator) \
105__ImplementPluginContext(ctx);\
106void ctx::Defragment()\
107{\
108 Graphics::GraphicsContext::InternalDefragment(idAllocator, std::forward<Graphics::GraphicsContextState>(__state));\
109}
110
111#define __CreatePluginContext() \
112 __state.Alloc = Alloc; \
113 __state.Dealloc = Dealloc;
114
115#define __CreateContext() \
116 __CreatePluginContext() \
117 __state.Defragment = Defragment;
118
119namespace Graphics
120{
121
122class View;
123struct FrameContext;
124
126{
127 // debug callbacks
128 void(*OnRenderDebug)(uint32_t flags);
129
130 // change callbacks
131 void(*OnViewCreated)(const Graphics::ViewId view);
132 void(*OnDiscardView)(const Graphics::ViewId view);
135 void(*OnViewportResized)(const uint framescriptHash, SizeT width, SizeT height);
136
138 OnAttachEntity(nullptr), OnRemoveEntity(nullptr), OnViewportResized(nullptr)
139 {
140 };
141};
142
144
146{
148
149 Util::Array<GraphicsEntityId> entities; // ContextEntityId -> GraphicsEntityId. kept adjacent to allocator data.
153 void(*Defragment)();
155 void(*OnInstanceMoved)(uint32_t toIndex, uint32_t fromIndex);
157 void(*OnDefragment)(uint32_t toIndex, uint32_t fromIndex);
158
160 {
161 while (!this->delayedRemoveQueue.IsEmpty())
162 {
163 Graphics::GraphicsEntityId eid = this->delayedRemoveQueue[0];
164 IndexT index = this->entitySliceMap.FindIndex(eid);
165 n_assert(index != InvalidIndex);
166 auto cid = this->entitySliceMap.ValueAtIndex(eid.id, index);
167 this->Dealloc(cid);
168 this->entitySliceMap.EraseIndex(eid, index);
169 this->delayedRemoveQueue.EraseIndexSwap(0);
170 }
171 }
172};
173
175{
176
177public:
181 virtual ~GraphicsContext();
182
183protected:
184 friend class GraphicsServer;
187 template<class ID_ALLOCATOR>
188 static void InternalDefragment(ID_ALLOCATOR& allocator, Graphics::GraphicsContextState&& state);
189};
190
191//------------------------------------------------------------------------------
194template<class ID_ALLOCATOR>
195inline void
197{
198 auto& freeIds = allocator.FreeIds();
199 Ids::Id32 index;
200 Ids::Id32 oldIndex;
202 IndexT mapIndex;
203 uint32_t dataSize;
204 SizeT size = freeIds.Size();
205 for (SizeT i = size - 1; i >= 0; --i)
206 {
207 index = freeIds.Back();
208 freeIds.EraseBack();
209 dataSize = (uint32_t)allocator.Size();
210 if (index >= dataSize)
211 {
212 continue;
213 }
214 oldIndex = dataSize - 1;
215 lastId = state.entities[oldIndex].id;
216 if (state.OnInstanceMoved != nullptr)
217 state.OnInstanceMoved(index, oldIndex);
218 allocator.EraseIndexSwap(index);
219 state.entities.EraseIndexSwap(index);
220 mapIndex = state.entitySliceMap.FindIndex(lastId);
221 if (mapIndex != InvalidIndex)
222 {
223 state.entitySliceMap.ValueAtIndex(lastId, mapIndex) = index;
224 }
225 else
226 {
227 freeIds.Append(index);
228 i++;
229 }
230 }
231 freeIds.Clear();
232}
233
234} // namespace Graphics
static void InternalDefragment(ID_ALLOCATOR &allocator, Graphics::GraphicsContextState &&state)
Definition graphicscontext.h:196
friend class GraphicsServer
Definition graphicscontext.h:184
static void InternalDeregisterEntity(const Graphics::GraphicsEntityId id, Graphics::GraphicsContextState &&state)
Definition graphicscontext.cc:52
static void InternalRegisterEntity(const Graphics::GraphicsEntityId id, Graphics::GraphicsContextState &&state)
Definition graphicscontext.cc:31
GraphicsContext()
constructor
Definition graphicscontext.cc:14
virtual ~GraphicsContext()
destructor
Definition graphicscontext.cc:22
A view describes a camera which can observe a Stage.
Nebula's dynamic array class.
Definition array.h:60
bool IsEmpty() const
return true if array empty
Definition array.h:1016
void EraseIndexSwap(IndexT index)
erase element at index, fill gap by swapping in last element, destroys sorting!
Definition array.h:1058
Organizes key/value pairs by a hash code.
Definition hashtable.h:42
IndexT FindIndex(const KEYTYPE &key) const
find index in bucket
Definition hashtable.h:580
VALUETYPE & ValueAtIndex(const KEYTYPE &key, IndexT i) const
get value from key and bucket
Definition hashtable.h:597
void EraseIndex(const KEYTYPE &key, IndexT i)
erase an entry with known index
Definition hashtable.h:533
#define n_assert(exp)
Definition debug.h:50
#define ID_32_TYPE(x)
Definition id.h:16
Implements the shader server used by Vulkan.
Definition cameramanager.h:22
uint32_t Id32
Definition id.h:138
id
Definition resourceid.h:37
Array< TYPE, STACK_SIZE > StackArray
Definition array.h:1890
Vulkan::GraphicsDeviceState state
Definition graphicscontext.h:143
void(* OnAttachEntity)(Graphics::GraphicsEntityId entity)
Definition graphicscontext.h:133
void(* OnRemoveEntity)(Graphics::GraphicsEntityId entity)
Definition graphicscontext.h:134
void(* OnDiscardView)(const Graphics::ViewId view)
Definition graphicscontext.h:132
GraphicsContextFunctionBundle()
Definition graphicscontext.h:137
void(* OnRenderDebug)(uint32_t flags)
Definition graphicscontext.h:128
void(* OnViewCreated)(const Graphics::ViewId view)
Definition graphicscontext.h:131
void(* OnViewportResized)(const uint framescriptHash, SizeT width, SizeT height)
Definition graphicscontext.h:135
A graphics context is a resource which holds a contextual representation for a graphics entity.
Definition graphicscontext.h:146
void(* OnDefragment)(uint32_t toIndex, uint32_t fromIndex)
called to manually handle fragmentation
Definition graphicscontext.h:157
void CleanupDelayedRemoveQueue()
Definition graphicscontext.h:159
void(* Dealloc)(ContextEntityId id)
Definition graphicscontext.h:152
void(* Defragment)()
Definition graphicscontext.h:153
void(* OnInstanceMoved)(uint32_t toIndex, uint32_t fromIndex)
called after a context entity has moved index
Definition graphicscontext.h:155
Util::StackArray< GraphicsEntityId, 8 > delayedRemoveQueue
Definition graphicscontext.h:147
Util::HashTable< GraphicsEntityId, ContextEntityId, 128, 64 > entitySliceMap
Definition graphicscontext.h:150
Util::Array< GraphicsEntityId > entities
Definition graphicscontext.h:149
ContextEntityId(* Alloc)()
Definition graphicscontext.h:151
The graphics entity is only an Id, to which we can attach GraphicsContexts.
Definition graphicsentity.h:16
Definition view.h:34
Typedefs for the Timing subsystem.
static const int InvalidIndex
Definition types.h:47
int SizeT
Definition types.h:42
unsigned int uint
Definition types.h:33
int IndexT
Definition types.h:41