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;
110struct FrameContext;
111
113{
114 // debug callbacks
115 void(*OnRenderDebug)(uint32_t flags);
116
117 // change callbacks
122 void(*OnViewportResized)(const uint framescriptHash, SizeT width, SizeT height);
123
125 OnAttachEntity(nullptr), OnRemoveEntity(nullptr), OnViewportResized(nullptr)
126 {
127 };
128};
129
131
133{
135
136 Util::Array<GraphicsEntityId> entities; // ContextEntityId -> GraphicsEntityId. kept adjacent to allocator data.
140 void(*Defragment)();
142 void(*OnInstanceMoved)(uint32_t toIndex, uint32_t fromIndex);
144 void(*OnDefragment)(uint32_t toIndex, uint32_t fromIndex);
145
147 {
148 while (!this->delayedRemoveQueue.IsEmpty())
149 {
150 Graphics::GraphicsEntityId eid = this->delayedRemoveQueue[0];
151 IndexT index = this->entitySliceMap.FindIndex(eid);
152 n_assert(index != InvalidIndex);
153 auto cid = this->entitySliceMap.ValueAtIndex(eid.id, index);
154 this->Dealloc(cid);
155 this->entitySliceMap.EraseIndex(eid, index);
156 this->delayedRemoveQueue.EraseIndexSwap(0);
157 }
158 }
159};
160
162{
163
164public:
168 virtual ~GraphicsContext();
169
170protected:
171 friend class GraphicsServer;
174 template<class ID_ALLOCATOR>
175 static void InternalDefragment(ID_ALLOCATOR& allocator, Graphics::GraphicsContextState&& state);
176};
177
178//------------------------------------------------------------------------------
181template<class ID_ALLOCATOR>
182inline void
184{
185 auto& freeIds = allocator.FreeIds();
186 Ids::Id32 index;
187 Ids::Id32 oldIndex;
189 IndexT mapIndex;
190 uint32_t dataSize;
191 SizeT size = freeIds.Size();
192 for (SizeT i = size - 1; i >= 0; --i)
193 {
194 index = freeIds.Back();
195 freeIds.EraseBack();
196 dataSize = (uint32_t)allocator.Size();
197 if (index >= dataSize)
198 {
199 continue;
200 }
201 oldIndex = dataSize - 1;
202 lastId = state.entities[oldIndex].id;
203 if (state.OnInstanceMoved != nullptr)
204 state.OnInstanceMoved(index, oldIndex);
205 allocator.EraseIndexSwap(index);
206 state.entities.EraseIndexSwap(index);
207 mapIndex = state.entitySliceMap.FindIndex(lastId);
208 if (mapIndex != InvalidIndex)
209 {
210 state.entitySliceMap.ValueAtIndex(lastId, mapIndex) = index;
211 }
212 else
213 {
214 freeIds.Append(index);
215 i++;
216 }
217 }
218 freeIds.Clear();
219}
220
221} // namespace Graphics
static void InternalDefragment(ID_ALLOCATOR &allocator, Graphics::GraphicsContextState &&state)
Definition graphicscontext.h:183
friend class GraphicsServer
Definition graphicscontext.h:171
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
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
Array< TYPE, STACK_SIZE > StackArray
Definition array.h:1890
Vulkan::GraphicsDeviceState state
Definition graphicscontext.h:130
void(* OnViewCreated)(const Ptr< Graphics::View > &view)
Definition graphicscontext.h:118
void(* OnAttachEntity)(Graphics::GraphicsEntityId entity)
Definition graphicscontext.h:120
void(* OnRemoveEntity)(Graphics::GraphicsEntityId entity)
Definition graphicscontext.h:121
void(* OnDiscardView)(const Ptr< Graphics::View > &view)
Definition graphicscontext.h:119
GraphicsContextFunctionBundle()
Definition graphicscontext.h:124
void(* OnRenderDebug)(uint32_t flags)
Definition graphicscontext.h:115
void(* OnViewportResized)(const uint framescriptHash, SizeT width, SizeT height)
Definition graphicscontext.h:122
A graphics context is a resource which holds a contextual representation for a graphics entity.
Definition graphicscontext.h:133
void(* OnDefragment)(uint32_t toIndex, uint32_t fromIndex)
called to manually handle fragmentation
Definition graphicscontext.h:144
void CleanupDelayedRemoveQueue()
Definition graphicscontext.h:146
void(* Dealloc)(ContextEntityId id)
Definition graphicscontext.h:139
void(* Defragment)()
Definition graphicscontext.h:140
void(* OnInstanceMoved)(uint32_t toIndex, uint32_t fromIndex)
called after a context entity has moved index
Definition graphicscontext.h:142
Util::StackArray< GraphicsEntityId, 8 > delayedRemoveQueue
Definition graphicscontext.h:134
Util::HashTable< GraphicsEntityId, ContextEntityId, 128, 64 > entitySliceMap
Definition graphicscontext.h:137
Util::Array< GraphicsEntityId > entities
Definition graphicscontext.h:136
ContextEntityId(* Alloc)()
Definition graphicscontext.h:138
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:47
int SizeT
Definition types.h:42
unsigned int uint
Definition types.h:33
int IndexT
Definition types.h:41