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 "graphicsentity.h"
27#include "coregraphics/window.h"
28
29#define __DeclarePluginContext() \
30private:\
31 static Graphics::GraphicsContextState __state;\
32 static Graphics::GraphicsContextFunctionBundle __bundle;\
33public:\
34 static void RegisterEntity(const Graphics::GraphicsEntityId id);\
35 static void DeregisterEntity(const Graphics::GraphicsEntityId id);\
36 static bool IsEntityRegistered(const Graphics::GraphicsEntityId id);\
37 static void Destroy(); \
38 static Graphics::ContextEntityId GetContextId(const Graphics::GraphicsEntityId id); \
39 static const Graphics::ContextEntityId& GetContextIdRef(const Graphics::GraphicsEntityId id); \
40 static void BeginBulkRegister(); \
41 static void EndBulkRegister(); \
42private:
43
44#define __DeclareContext() \
45 __DeclarePluginContext(); \
46 static void Defragment();
47
48
49#define __ImplementPluginContext(ctx) \
50Graphics::GraphicsContextState ctx::__state; \
51Graphics::GraphicsContextFunctionBundle ctx::__bundle; \
52void ctx::RegisterEntity(const Graphics::GraphicsEntityId id) \
53{\
54 Graphics::GraphicsContext::InternalRegisterEntity(id, std::forward<Graphics::GraphicsContextState>(__state));\
55}\
56\
57void ctx::DeregisterEntity(const Graphics::GraphicsEntityId id)\
58{\
59 Graphics::GraphicsContext::InternalDeregisterEntity(id, std::forward<Graphics::GraphicsContextState>(__state));\
60}\
61\
62bool ctx::IsEntityRegistered(const Graphics::GraphicsEntityId id)\
63{\
64 return __state.entitySliceMap.Contains(id);\
65}\
66void ctx::Destroy()\
67{\
68 Graphics::GraphicsServer::Instance()->UnregisterGraphicsContext(&__bundle);\
69}\
70Graphics::ContextEntityId ctx::GetContextId(const Graphics::GraphicsEntityId id)\
71{\
72 IndexT idx = __state.entitySliceMap.FindIndex(id); \
73 if (idx == InvalidIndex) return Graphics::InvalidContextEntityId; \
74 else return __state.entitySliceMap.ValueAtIndex(id, idx); \
75}\
76const Graphics::ContextEntityId& ctx::GetContextIdRef(const Graphics::GraphicsEntityId id)\
77{\
78 IndexT idx = __state.entitySliceMap.FindIndex(id); \
79 n_assert(idx != InvalidIndex); \
80 return __state.entitySliceMap.ValueAtIndex(id, idx); \
81}\
82void ctx::BeginBulkRegister()\
83{\
84 __state.entitySliceMap.BeginBulkAdd();\
85}\
86void ctx::EndBulkRegister()\
87{\
88 __state.entitySliceMap.EndBulkAdd();\
89}
90
91#define __ImplementContext(ctx, idAllocator) \
92__ImplementPluginContext(ctx);\
93void ctx::Defragment()\
94{\
95 Graphics::GraphicsContext::InternalDefragment(idAllocator, std::forward<Graphics::GraphicsContextState>(__state));\
96}
97
98#define __CreatePluginContext() \
99 __state.Alloc = Alloc; \
100 __state.Dealloc = Dealloc;
101
102#define __CreateContext() \
103 __CreatePluginContext() \
104 __state.Defragment = Defragment;
105
106namespace Graphics
107{
108
109class View;
110class Stage;
111struct FrameContext;
112
114{
115 // debug callbacks
116 void(*OnRenderDebug)(uint32_t flags);
117
118 // change callbacks
125 void(*OnWindowResized)(const CoreGraphics::WindowId windowId, SizeT width, SizeT height);
126
128 OnAttachEntity(nullptr), OnRemoveEntity(nullptr), OnWindowResized(nullptr)
129 {
130 };
131};
132
134
136{
138
139 Util::Array<GraphicsEntityId> entities; // ContextEntityId -> GraphicsEntityId. kept adjacent to allocator data.
142 void(*Dealloc)(ContextEntityId id);
143 void(*Defragment)();
145 void(*OnInstanceMoved)(uint32_t toIndex, uint32_t fromIndex);
147 void(*OnDefragment)(uint32_t toIndex, uint32_t fromIndex);
148
150 {
151 while (!this->delayedRemoveQueue.IsEmpty())
152 {
153 Graphics::GraphicsEntityId eid = this->delayedRemoveQueue[0];
154 IndexT index = this->entitySliceMap.FindIndex(eid);
155 n_assert(index != InvalidIndex);
156 auto cid = this->entitySliceMap.ValueAtIndex(eid.id, index);
157 this->Dealloc(cid);
158 this->entitySliceMap.EraseIndex(eid, index);
159 this->delayedRemoveQueue.EraseIndexSwap(0);
160 }
161 }
162};
163
165{
166
167public:
171 virtual ~GraphicsContext();
172
173protected:
174 friend class GraphicsServer;
177 template<class ID_ALLOCATOR>
178 static void InternalDefragment(ID_ALLOCATOR& allocator, Graphics::GraphicsContextState&& state);
179};
180
181//------------------------------------------------------------------------------
184template<class ID_ALLOCATOR>
185inline void
187{
188 auto& freeIds = allocator.FreeIds();
189 Ids::Id32 index;
190 Ids::Id32 oldIndex;
192 IndexT mapIndex;
193 uint32_t dataSize;
194 SizeT size = freeIds.Size();
195 for (SizeT i = size - 1; i >= 0; --i)
196 {
197 index = freeIds.Back();
198 freeIds.EraseBack();
199 dataSize = (uint32_t)allocator.Size();
200 if (index >= dataSize)
201 {
202 continue;
203 }
204 oldIndex = dataSize - 1;
205 lastId = state.entities[oldIndex].id;
206 if (state.OnInstanceMoved != nullptr)
207 state.OnInstanceMoved(index, oldIndex);
208 allocator.EraseIndexSwap(index);
209 state.entities.EraseIndexSwap(index);
210 mapIndex = state.entitySliceMap.FindIndex(lastId);
211 if (mapIndex != InvalidIndex)
212 {
213 state.entitySliceMap.ValueAtIndex(lastId, mapIndex) = index;
214 }
215 else
216 {
217 freeIds.Append(index);
218 i++;
219 }
220 }
221 freeIds.Clear();
222}
223
224} // namespace Graphics
Definition graphicscontext.h:165
static void InternalDefragment(ID_ALLOCATOR &allocator, Graphics::GraphicsContextState &&state)
Definition graphicscontext.h:186
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
The graphics server is the main singleton for the Graphics subsystem.
Definition graphicsserver.h:47
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
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:21
uint32_t Id32
Definition id.h:138
id
Definition resourceid.h:37
Vulkan::GraphicsDeviceState state
Definition memory.h:41
Definition window.h:25
Definition graphicscontext.h:133
Definition graphicscontext.h:114
void(* OnViewCreated)(const Ptr< Graphics::View > &view)
Definition graphicscontext.h:121
void(* OnAttachEntity)(Graphics::GraphicsEntityId entity)
Definition graphicscontext.h:123
void(* OnRemoveEntity)(Graphics::GraphicsEntityId entity)
Definition graphicscontext.h:124
void(* OnWindowResized)(const CoreGraphics::WindowId windowId, SizeT width, SizeT height)
Definition graphicscontext.h:125
void(* OnDiscardView)(const Ptr< Graphics::View > &view)
Definition graphicscontext.h:122
GraphicsContextFunctionBundle()
Definition graphicscontext.h:127
void(* OnRenderDebug)(uint32_t flags)
Definition graphicscontext.h:116
void(* OnStageCreated)(const Ptr< Graphics::Stage > &stage)
Definition graphicscontext.h:119
void(* OnDiscardStage)(const Ptr< Graphics::Stage > &stage)
Definition graphicscontext.h:120
A graphics context is a resource which holds a contextual representation for a graphics entity.
Definition graphicscontext.h:136
void CleanupDelayedRemoveQueue()
Definition graphicscontext.h:149
Util::StackArray< GraphicsEntityId, 8 > delayedRemoveQueue
Definition graphicscontext.h:137
Util::HashTable< GraphicsEntityId, ContextEntityId, 128, 64 > entitySliceMap
Definition graphicscontext.h:140
Util::Array< GraphicsEntityId > entities
Definition graphicscontext.h:139
The graphics entity is only an Id, to which we can attach GraphicsContexts.
Definition graphicsentity.h:16
Typedefs for the Timing subsystem.
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48