Cogs.Core
RenderList.h
1#pragma once
2
3#include "Resources/MaterialInstance.h"
4
5#include "IRenderer.h"
6#include "RenderMesh.h"
7#include "RenderStates.h"
8#include "RenderResource.h"
9#include "../Components/Core/RenderComponent.h"
10
11#include "Foundation/Geometry/BoundingBox.hpp"
12
13namespace Cogs::Core
14{
15 struct RenderTaskContext;
16
17 enum struct RenderItemFlags : uint16_t
18 {
19 None = 0,
20 Custom = 1 << 0,
21 CastShadows = 1 << 1,
22 CalculateDepth = 1 << 2,
23 Sprite = 1 << 3,
24 AlwaysOnTop = 1 << 4,
25 Clockwise = 1 << 5,
26 Transparent = 1 << 6,
27 Backdrop = 1 << 7,
28 CustomBucket = 1 << 8,
29 CustomBounds = 1 << 9,
30 Custom2 = 1 << 10,
31 DisableCulling = 1 << 11,
32 Instanced = 1 << 12,
33 ObjectBufferSlot = 1 << 13
34 };
35 ENABLE_ENUM_FLAGS(RenderItemFlags);
36
37 enum struct StateChangeFlags : uint16_t {
38 ChangeNone = 0,
39 ChangeTransform = 1 << 0,
40 ChangeMesh = 1 << 1,
41 ChangeMaterialInstance = 1 << 2,
42 ChangeMaterialVariant = 1 << 3,
43 ChangeRasterizerState = 1 << 4,
44 ChangeBlendState = 1 << 5,
45 ChangeDepthStencilState = 1 << 6,
46 ChangeLayer = 1 << 7,
47 ChangeViewport = 1 << 8,
48 ChangeViewportData = 1 << 9,
49 ChangeAll = 0xFFFF
50 };
51 ENABLE_ENUM_FLAGS(StateChangeFlags);
52
53 enum struct BucketType
54 {
55 Backdrop = 0,
57 Solid,
59 Transparent,
60 Overlay,
61 Custom,
62 Count
63 };
64
65 enum class BucketMask
66 {
67 BackDrop = 1 << size_t(BucketType::Backdrop),
68 Transparent = 1 << size_t(BucketType::Transparent),
69 Solid = (1 << size_t(BucketType::SolidOrderedBack)) | (1 << size_t(BucketType::Solid)) | (1 << size_t(BucketType::SolidOrderedFront)),
70 Opaque = Solid,
71 Overlay = 1 << size_t(BucketType::Overlay),
72 Custom = 1 << size_t(BucketType::Custom),
73 All = ~0,
74 None = 0,
75 };
76 ENABLE_ENUM_FLAGS(BucketMask);
77
78 inline bool hasBucket(BucketMask bucketMask, BucketType bucket)
79 {
80 return (size_t(bucketMask) & (size_t(1) << size_t(bucket))) != 0;
81 }
82
84 {
85 typedef void CustomCallback(RenderTaskContext*, DrawContext*, const RenderItem*);
86 typedef void CustomCallback2(RenderTaskContext*, DrawContext*, struct RenderListTask* task, const RenderItem*);
87
88
89 bool isCustom() const { return (flags & (RenderItemFlags::Custom | RenderItemFlags::Custom2)) != 0; }
90 bool isCustom1() const { return (flags & RenderItemFlags::Custom) != 0; }
91 bool isCustom2() const { return (flags & RenderItemFlags::Custom2) != 0; }
92 bool isSprite() const { return (flags & RenderItemFlags::Sprite) != 0; }
93 bool isClockwise() const { return (flags & RenderItemFlags::Clockwise) != 0; }
94 bool isTransparent() const { return (flags & RenderItemFlags::Transparent) != 0; }
95 bool isCustomBucket() const { return (flags & RenderItemFlags::CustomBucket) != 0; }
96 bool isBackdrop() const { return (flags & RenderItemFlags::Backdrop) != 0; }
97 bool needsDepthCalculation() const { return (flags & RenderItemFlags::CalculateDepth) != 0; }
98 bool hasBoundingBox() const { return (flags & RenderItemFlags::CustomBounds) != 0; }
99 bool isCulled() const { return !hasBoundingBox() && !isCustom(); }
100 bool isInstanced() const { return (flags & RenderItemFlags::Instanced) != 0; }
101 bool hasObjectBufferSlot() const { return (flags & RenderItemFlags::ObjectBufferSlot) != 0; }
102
103 void setBounds(const Geometry::BoundingBox* bounds)
104 {
105 boundingBox = bounds;
106 flags |= RenderItemFlags::CustomBounds;
107 }
108
109 template<typename T>
110 void setCallbackData(T* t) const { callbackData = t; }
111
112 template<typename T>
113 void setCallbackData2(T* t) const { callbackData2 = t; }
114
115 template<typename T>
116 T* getCallbackData() const { return static_cast<T*>(callbackData); }
117
118 template<typename T>
119 T* getCallbackData2() const { return static_cast<T*>(callbackData2); }
120
121
122
123 const struct RenderMaterialInstance * renderMaterialInstance = nullptr;
124 const MaterialInstance * materialInstance = nullptr;
125
126 const struct EffectBinding * binding = nullptr;
127 const struct CameraData * viewportData = nullptr;
128 const struct PoseData * poseData = nullptr;
129 union
130 {
131 uint32_t cullingIndex;
132 const Geometry::BoundingBox* boundingBox = nullptr;
133 };
134
135 VertexArrayObjectHandle vertexArrayObject = VertexArrayObjectHandle::NoHandle; // FIXME: input layout and vao is mutually exclusive
136
137 union
138 {
140 struct
141 {
142 const glm::mat4* worldMatrix;
143
144 const RenderMesh* meshData;
145 uint32_t startIndex;
146 uint32_t numIndexes;
147
148 const RenderMesh* instanceData;
149 uint32_t startInstance;
150 uint32_t numInstances;
151
153 };
154
156 struct
157 {
158 const MeshStreamsLayout* streamsLayout;
159 union
160 {
161 CustomCallback* callback;
162 CustomCallback2* callback2;
163 };
164 mutable void* callbackData;
165 mutable void* callbackData2;
166 };
167 };
168
170 RenderLayers layer = RenderLayers::None;
171 float depth = 0.f;
172 int32_t drawOrder = 0;
173 union {
174 uint32_t objectId = 0;
176 };
177 RenderItemFlags flags = RenderItemFlags::None;
178 StateChangeFlags stateChanges = StateChangeFlags::ChangeAll;
179
180 // Pipeline state
181 //
182 // Note: These will all be merged into one state handle moving to PSO in future
183 // graphics APIs.
184 uint16_t blendState = 0;
185 uint16_t depthState = 0;
186 uint16_t rasterizerState = 0;
187 uint8_t clipShapeIx = 0;
188
189 size_t hash(size_t hashValue = Cogs::hash()) const {
190 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(renderMaterialInstance), hashValue);
191 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(materialInstance), hashValue);
192 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(binding), hashValue);
193 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(viewportData), hashValue);
194 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(poseData), hashValue);
195
196 if (hasBoundingBox()) {
197 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(boundingBox), hashValue);
198 }
199 else {
200 hashValue = Cogs::hash(cullingIndex, hashValue);
201 }
202 hashValue = Cogs::hash(vertexArrayObject.handle);
203
204 if (isCustom()) {
205 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(worldMatrix), hashValue);
206 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(meshData), hashValue);
207 hashValue = Cogs::hash(startIndex, hashValue);
208 hashValue = Cogs::hash(numIndexes, hashValue);
209 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(instanceData), hashValue);
210 hashValue = Cogs::hash(startInstance, hashValue);
211 hashValue = Cogs::hash(numInstances, hashValue);
212 hashValue = Cogs::hash(primitiveType, hashValue);
213 }
214 else {
215 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(streamsLayout), hashValue);
216 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(callback), hashValue);
217 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(callbackData), hashValue);
218 hashValue = Cogs::hash(reinterpret_cast<intptr_t>(callbackData2), hashValue);
219 }
220
221 hashValue = Cogs::hash(lod.selectedLod, hashValue);
222 hashValue = Cogs::hash(lod.numLods, hashValue);
223 hashValue = Cogs::hash(lod.currentLod, hashValue);
224 hashValue = Cogs::hash(lod.lodFraction, hashValue);
225 hashValue = Cogs::hash(layer, hashValue);
226 hashValue = Cogs::hash(depth, hashValue);
227 hashValue = Cogs::hash(drawOrder, hashValue);
228 hashValue = Cogs::hash(objectId, hashValue);
229 hashValue = Cogs::hash(flags, hashValue);
230 hashValue = Cogs::hash(stateChanges, hashValue);
231 hashValue = Cogs::hash(blendState, hashValue);
232 hashValue = Cogs::hash(depthState, hashValue);
233 hashValue = Cogs::hash(rasterizerState, hashValue);
234 hashValue = Cogs::hash(clipShapeIx, hashValue);
235
236 return hashValue;
237 }
238 };
239
240 typedef std::vector<RenderItem> RenderItems;
241
242
243
245 {
246 RenderBatch() : allItems(1024)
247 {
248 current = allItems.data();
249 }
250
251 RenderItem * current = nullptr;
252
253 RenderItem * begin() { return allItems.data(); }
254 RenderItem * end() { return current; }
255 const RenderItem * begin() const { return allItems.data(); }
256 const RenderItem * end() const { return current; }
257 size_t size() const { return size_t(current - begin()); }
258 bool empty() const { return begin() == end(); }
259 RenderItem& operator[](size_t idx)
260 {
261 assert(idx < size());
262 return begin()[idx];
263 }
264 const RenderItem& operator[](size_t idx) const
265 {
266 assert(idx < size());
267 return begin()[idx];
268 }
269
270 void clear() { current = allItems.data(); }
271
272 RenderItem& alloc()
273 {
274 const size_t sz = size();
275 if (sz == allItems.size()) {
276 allItems.resize(allItems.size() * 2);
277 current = allItems.data() + sz;
278 }
279 return *current++;
280 }
281
282 RenderItem & createCustom()
283 {
284 RenderItem& item = alloc();
285 item = {};
286 item.cullingIndex = (uint32_t)-1;
287 item.flags = RenderItemFlags::Custom;
288 return item;
289 }
290
291 private:
292 RenderItems allItems;
293 public:
294 size_t hash = 0;
295 size_t generation = 0;
296 size_t missing = (size_t)-1;
297 };
298
300 {
301 RenderList() : batches({ &batch }) {}
302
303 RenderItem * begin() { return batch.begin(); }
304 RenderItem * end() { return batch.end(); }
305 const RenderItem * begin() const { return batch.begin(); }
306 const RenderItem * end() const { return batch.end(); }
307 size_t size() const { return batch.size(); }
308 void clear()
309 {
310 batch.clear();
311 batches = { &batch };
312 }
313
314 RenderItem & createCustom(const MeshStreamsLayout* streamsLayout)
315 {
316 RenderItem& rv = batch.createCustom();
317 rv.streamsLayout = streamsLayout;
318 return rv;
319 }
320
321 RenderBatch batch;
322
323 std::vector<RenderBatch *> batches;
324
325 RenderItems buckets[size_t(BucketType::Count)];
326
327 size_t hash = 0;
328 const struct CameraData * viewportData = nullptr;
329 struct ListObjectBuffer* listObjectBuffer = nullptr;
330 struct ClipShapeCache* clipShapeCache = nullptr;
331 };
332
333}
334template<> inline Cogs::StringView getName<Cogs::Core::StateChangeFlags>() { return "StateChangeFlags"; }
335template<> inline Cogs::StringView getName<Cogs::Core::BucketMask>() { return "BucketMask"; }
336
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
@ CastShadows
Casts shadows.
@ DisableCulling
Disable culling and always force this component to be rendered.
@ SolidOrderedBack
Opaque ordered geometry behind un-ordered geometry.
@ Transparent
Transparent geometry rendered after the opaque geometry.
@ SolidOrderedFront
Opaque ordered geometry in front of un-ordered geometry.
@ Solid
Opaque geometry in arbitrary order.
@ Backdrop
Background geometry behind everything else.
RenderLayers
Contains common render layers.
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
Contains data describing a Camera instance and its derived data structured such as matrix data and vi...
Definition: CameraSystem.h:67
Material instances represent a specialized Material combined with state for all its buffers and prope...
Lod data holding the LoD state of the entity this render component belongs to.
uint8_t numLods
Total number of LoDs available.
uint8_t currentLod
The assigned LoD of the current component.
uint8_t selectedLod
The selected LoD of the composite entity.
uint32_t objectBufferSlot
Set if ObjectBufferSlot is set (then transform and objectId is in the object buffer).
Definition: RenderList.h:175
int32_t drawOrder
Ordering of draw items within a bucket.
Definition: RenderList.h:172
uint32_t objectId
Lower 6 of upper 8 bits are instance id bits, lower 24 bits are object id.
Definition: RenderList.h:174
RenderLayers layer
Visibility mask.
Definition: RenderList.h:170
StateChangeFlags stateChanges
Encodes what state changed from previous item, updated by FilterListTask.
Definition: RenderList.h:178
float depth
Used for depth-sorting of transparent objects.
Definition: RenderList.h:171
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:77
handle_type handle
Internal resource handle.
Definition: Common.h:74
EPrimitiveType
Primitive type enumeration.
Definition: Common.h:114