Repository navigation
Expand file tree
/
Copy pathStandard2DRenderer.cpp
More file actions
725 lines (652 loc) · 26.3 KB
/
Copy pathStandard2DRenderer.cpp
File metadata and controls
725 lines (652 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include "Standard2DRenderer.h"
#include "RenderProfiler.h"
#include "DekiRendererRegistry.h"
#include <deki/IClipProvider.h>
#include <deki/ISortableProvider.h>
#include <deki/Engine.h>
#include <deki/SceneSystem.h>
#include "CameraComponent.h"
#include "RendererComponent.h"
#include "QuadBlit.h"
#include <deki/Object.h>
#include <deki/Scene.h>
#include <deki/LogSystem.h>
#include <deki/providers/Memory.h>
#include <algorithm>
#include <cmath>
namespace DekiRendering
{
// Registers this renderer with the renderer registry.
static struct Standard2DRegistrar
{
Standard2DRegistrar()
{
DekiRendererRegistry::Register("standard2d", []() -> DekiRenderer* { return new Standard2DRenderer(); });
}
} s_Standard2dRegistrar;
// --- Passes and callbacks ---
void Standard2DRenderer::AddPass(RenderPass* pass)
{
if (pass && std::find(m_Passes.begin(), m_Passes.end(), pass) == m_Passes.end())
{
m_Passes.push_back(pass);
}
}
void Standard2DRenderer::RemovePass(RenderPass* pass)
{
auto it = std::find(m_Passes.begin(), m_Passes.end(), pass);
if (it != m_Passes.end())
{
m_Passes.erase(it);
}
}
bool Standard2DRenderer::DrawsInStrips(Deki::Scene* scene) const
{
for (const RenderPass* pass : m_Passes)
{
if (!pass->DrawsInStrips(scene))
{
return false;
}
}
return true;
}
bool Standard2DRenderer::CoversView(const RenderContext& ctx) const
{
// A later strip replays what the first recorded, cover included. The
// first strip is not known until it is drawn, so it is cleared.
return m_PlanCovers && ctx.contentFrame != 0 && ctx.contentFrame == m_PlanFrame;
}
void Standard2DRenderer::AddSortingCallback(SortingCallback cb)
{
if (cb && std::find(m_SortingCallbacks.begin(), m_SortingCallbacks.end(), cb) == m_SortingCallbacks.end())
{
m_SortingCallbacks.push_back(cb);
}
}
void Standard2DRenderer::RemoveSortingCallback(SortingCallback cb)
{
auto it = std::find(m_SortingCallbacks.begin(), m_SortingCallbacks.end(), cb);
if (it != m_SortingCallbacks.end())
{
m_SortingCallbacks.erase(it);
}
}
void Standard2DRenderer::RebuildHookLists()
{
m_BeginPasses.clear();
m_PrePasses.clear();
m_ExecPasses.clear();
m_PostPasses.clear();
m_EndPasses.clear();
for (RenderPass* pass : m_Passes)
{
const uint32_t mask = pass->HookMask();
if (mask & RenderPassHooks::BeginFrame)
{
m_BeginPasses.push_back(pass);
}
if (mask & RenderPassHooks::PreExecute)
{
m_PrePasses.push_back(pass);
}
if (mask & RenderPassHooks::Execute)
{
m_ExecPasses.push_back(pass);
}
if (mask & RenderPassHooks::PostExecute)
{
m_PostPasses.push_back(pass);
}
if (mask & RenderPassHooks::EndFrame)
{
m_EndPasses.push_back(pass);
}
}
}
// --- Component classification ---
const Standard2DRenderer::TypeTraits& Standard2DRenderer::TraitsFor(const Deki::Component* comp)
{
const Deki::ComponentType type = comp->GetType();
auto it = m_TypeTraits.find(type);
if (it != m_TypeTraits.end())
{
return it->second;
}
// Same predicates as Deki::Object::GetComponent<RendererComponent>() and
// FindInterface<T>() (exact type, then one base level), evaluated once.
const Deki::ComponentType base = comp->GetBaseType();
TypeTraits traits;
traits.isRenderer = (type == ::Deki::TypeId<RendererComponent>() || base == ::Deki::TypeId<RendererComponent>());
traits.clipAdapter = Deki::ComponentInterfaceAdapters::Find(Deki::IClipProvider::kInterfaceID, type, base);
traits.sortableAdapter = Deki::ComponentInterfaceAdapters::Find(Deki::ISortableProvider::kInterfaceID, type, base);
return m_TypeTraits.emplace(type, traits).first->second;
}
Standard2DRenderer::Renderables Standard2DRenderer::ResolveRenderables(Deki::Object* obj)
{
// One walk of the component list; the first match wins for each role, the
// same result as separate GetComponent / FindInterface lookups.
Renderables r{ nullptr, nullptr, nullptr };
for (Deki::Component* comp : obj->GetComponents())
{
const TypeTraits& t = TraitsFor(comp);
if (t.isRenderer && !r.renderer)
{
r.renderer = static_cast<RendererComponent*>(comp);
}
if (t.clipAdapter && !r.clip)
{
r.clip = static_cast<Deki::IClipProvider*>(t.clipAdapter(comp));
}
if (t.sortableAdapter && !r.sortable)
{
r.sortable = static_cast<Deki::ISortableProvider*>(t.sortableAdapter(comp));
}
}
return r;
}
// --- Built-in components ---
void Standard2DRenderer::ExecuteBuiltins(const SortItem& item, RenderContext& ctx, BuiltinGeometry* geo)
{
Deki::Object* obj = item.obj;
// Positions are worked out in the whole view, exactly as for a whole
// frame, and only then moved to the strip by whole pixels, so a strip
// truncates and rounds every edge the way the whole frame does. (ctx.cam
// is the strip's: the whole view's moved up by viewTop and left by
// viewLeft, which is exact.)
FrameCamera cam = ctx.cam;
cam.halfH += static_cast<float>(ctx.viewTop);
cam.halfW += static_cast<float>(ctx.viewLeft);
const int32_t viewTop = ctx.viewTop;
const int32_t viewLeft = ctx.viewLeft;
// The whole-view geometry: the same in every strip of a frame, so a later
// strip takes it from the first.
BuiltinGeometry local;
BuiltinGeometry& g = geo ? *geo : local;
if (!g.valid)
{
g.wt = obj->GetWorldTransform(); // one dirty check for all five values
cam.WorldToScreen(g.wt.x, g.wt.y, g.screenX, g.screenY);
if (item.clip)
{
const int32_t screenX = static_cast<int32_t>(std::floor(g.screenX));
const int32_t screenY = static_cast<int32_t>(std::floor(g.screenY));
const float scaledW = item.clip->GetClipWidth() * cam.ppm * g.wt.scaleX;
const float scaledH = item.clip->GetClipHeight() * cam.ppm * g.wt.scaleY;
g.clipLeft = screenX - static_cast<int32_t>(std::floor(scaledW * 0.5f));
g.clipTop = screenY - static_cast<int32_t>(std::floor(scaledH * 0.5f));
g.clipWidth = static_cast<int32_t>(scaledW);
g.clipHeight = static_cast<int32_t>(scaledH);
}
// The cull box is conservative, from the component's world extents:
// it allows any pivot (the content lies within one full size of the
// origin) and any rotation (within width + height). Objects of
// unknown size are drawn.
float extentW = 0.0f, extentH = 0.0f;
g.hasExtents = item.renderer && item.renderer->GetContentExtents(extentW, extentH);
if (g.hasExtents)
{
g.reach = (std::fabs(extentW * g.wt.scaleX) + std::fabs(extentH * g.wt.scaleY)) * cam.ppm + 2.0f;
}
g.valid = true;
}
const Deki::WorldTransform& wt = g.wt;
// Clip: push a clip rect if the object has an IClipProvider.
if (item.clip)
{
const int32_t left = g.clipLeft - viewLeft;
const int32_t top = g.clipTop - viewTop;
QuadBlit::PushClipRect(left, top, left + g.clipWidth, top + g.clipHeight);
}
// Renderer (a sprite, say): blit its content.
RendererComponent* renderer = item.renderer;
if (renderer)
{
const bool useOrderedDither = (renderer->alphaMode == AlphaMode::OrderedDither);
float fScreenX = g.screenX, fScreenY = g.screenY;
// A strip outside the rows an earlier strip found the blit writes:
// nothing to draw here.
if (g.areaKnown && (g.areaBottom <= viewTop || g.areaTop >= viewTop + ctx.height || g.areaRight <= viewLeft ||
g.areaLeft >= viewLeft + ctx.width))
{
return;
}
// Cull before RenderContent, which may rasterise text, bake a gradient
// or copy a frame.
if (g.hasExtents)
{
const float cx = fScreenX - static_cast<float>(viewLeft), cy = fScreenY - static_cast<float>(viewTop);
const float reach = g.reach;
float left = 0.0f, top = 0.0f;
float right = static_cast<float>(ctx.width), bottom = static_cast<float>(ctx.height);
if (!renderer->ignoreClip && QuadBlit::IsClipEnabled())
{
const QuadBlit::ClipRect clip = QuadBlit::GetCurrentClipRect();
left = std::max(left, static_cast<float>(clip.left));
top = std::max(top, static_cast<float>(clip.top));
right = std::min(right, static_cast<float>(clip.right));
bottom = std::min(bottom, static_cast<float>(clip.bottom));
}
if (cx + reach < left || cx - reach > right || cy + reach < top || cy - reach > bottom)
{
return;
}
}
QuadBlit::Source source;
float pivotX, pivotY;
uint8_t tintR, tintG, tintB, tintA;
const bool profiling = RenderProfiler::IsEnabled();
bool hasContent = false;
// Drawn strip by strip, an object across several strips produces its
// content once (a particle composite, a text bake) and every strip
// blits that. Content handed over with ownsPixels is freed after its
// blit, so it is produced again for each strip.
CachedContent* cached = nullptr;
if (ctx.contentFrame != 0)
{
auto it = m_ContentCache.find(renderer);
if (it != m_ContentCache.end())
{
cached = &it->second;
}
}
if (cached)
{
hasContent = cached->hasContent;
source = cached->source;
pivotX = cached->pivotX;
pivotY = cached->pivotY;
tintR = cached->tintR;
tintG = cached->tintG;
tintB = cached->tintB;
tintA = cached->tintA;
}
else
{
const uint64_t contentStart = profiling ? RenderProfiler::Now() : 0;
hasContent = renderer->RenderContent(obj, source, pivotX, pivotY, tintR, tintG, tintB, tintA);
if (profiling)
{
const Deki::ComponentTypeInfo* info = renderer->GetTypeInfo();
RenderProfiler::AddContent(renderer->GetType(), RenderProfiler::Now() - contentStart,
info ? info->shortName : nullptr);
}
if (ctx.contentFrame != 0 && !(hasContent && source.ownsPixels))
{
m_ContentCache[renderer] =
CachedContent{ hasContent, source, pivotX, pivotY, tintR, tintG, tintB, tintA };
}
}
if (hasContent)
{
// ignoreClip: switch clipping off for this blit.
bool wasClipEnabled = QuadBlit::IsClipEnabled();
if (renderer->ignoreClip)
{
QuadBlit::SetClipEnabled(false);
}
// Units: source pixels -> world meters -> screen pixels.
// screen_px = (source_px / source.pixelsPerMeter) * world_scale * camera.pixelsPerMeter
// QuadBlit applies (source_px * scale), so:
// scale = world_scale * camera.pixelsPerMeter / source.pixelsPerMeter
//
// World coordinates are always meters. Source art renders 1:1 when
// the camera's, the sprite's and the project's pixels-per-meter match.
const float worldToScreen = cam.ppm;
const float spritePPM = (source.pixelsPerMeter > 0.0f) ? source.pixelsPerMeter : 1.0f;
const float invSourcePPM = 1.0f / spritePPM;
const float drawScaleX = wt.scaleX * worldToScreen * invSourcePPM;
const float drawScaleY = wt.scaleY * worldToScreen * invSourcePPM;
// Pixel Perfect: snap to the art-pixel grid, whatever the
// renderer's own setting. Otherwise:
// pixelSnap true: round to the nearest pixel (sharp sprite art).
// pixelSnap false: truncate, so sub-pixel motion accumulates and
// continuous movement looks smoother (there is no bilinear yet).
if (cam.snapStep > 0)
{
fScreenX = cam.SnapX(fScreenX);
fScreenY = cam.SnapY(fScreenY);
}
const int32_t intScreenX =
((renderer->pixelSnap || cam.snapStep > 0) ? static_cast<int32_t>(std::lround(fScreenX))
: static_cast<int32_t>(fScreenX)) -
viewLeft;
const int32_t intScreenY =
((renderer->pixelSnap || cam.snapStep > 0) ? static_cast<int32_t>(std::lround(fScreenY))
: static_cast<int32_t>(fScreenY)) -
viewTop;
// Recording a frame's first strip: does this draw cover the whole
// view? (Content handed over with ownsPixels is produced again in
// each strip, so it may not be the same there.)
if (m_PlanMode == PlanMode::Recording && !m_PlanCovers && ctx.buffer == m_FrameBuffer && !source.ownsPixels)
{
const int32_t viewWidth = ctx.viewWidth > 0 ? ctx.viewWidth : ctx.width;
const int32_t viewHeight = ctx.viewHeight > 0 ? ctx.viewHeight : ctx.height;
m_PlanCovers = QuadBlit::CoversRect(source, intScreenX, intScreenY, drawScaleX, drawScaleY, wt.rotation,
pivotX, pivotY, tintA, -viewLeft, -viewTop, viewWidth - viewLeft,
viewHeight - viewTop);
}
// What it writes, for the frame's other strips (their content is
// this one: content made again for each strip may not be).
if (geo && !g.areaKnown && ctx.contentFrame != 0 && !source.ownsPixels)
{
int32_t left = 0, top = 0, right = 0, bottom = 0;
if (!QuadBlit::BlitArea(source, intScreenX, intScreenY, drawScaleX, drawScaleY, wt.rotation, pivotX,
pivotY, left, top, right, bottom))
{
left = top = right = bottom = 0;
}
g.areaLeft = left + viewLeft;
g.areaTop = top + viewTop;
g.areaRight = right + viewLeft;
g.areaBottom = bottom + viewTop;
g.areaKnown = true;
}
const uint64_t blitStart = profiling ? RenderProfiler::Now() : 0;
QuadBlit::Blit(source, ctx.buffer, ctx.width, ctx.height, ctx.format, intScreenX, intScreenY, drawScaleX,
drawScaleY, wt.rotation, pivotX, pivotY, tintR, tintG, tintB, tintA, useOrderedDither);
if (profiling)
{
RenderProfiler::AddBlit(renderer->GetType(), RenderProfiler::Now() - blitStart,
static_cast<uint64_t>(source.width) * static_cast<uint64_t>(source.height));
}
if (renderer->ignoreClip)
{
QuadBlit::SetClipEnabled(wasClipEnabled);
}
// A component that composed its pixels this frame can hand the
// buffer over instead of keeping one; freeing it is then our job.
//
// Through Deki::Memory, not delete[]: the buffer came from the
// engine's allocator (Deki::Buffer<T>::Release(), or Allocate),
// and delete[] would misread its header. Every component in the
// tree keeps its own buffer, so only third-party ones reach this.
if (source.ownsPixels && source.pixels)
{
Deki::Memory::Free(const_cast<uint8_t*>(source.pixels));
}
}
}
}
void Standard2DRenderer::PostExecuteBuiltins(const SortItem& item)
{
// Pop the clip rect pushed in ExecuteBuiltins.
if (item.clip)
{
QuadBlit::PopClipRect();
}
}
// --- Sortable item collection ---
std::vector<Standard2DRenderer::SortItem>& Standard2DRenderer::SortListForDepth(int depth)
{
while (static_cast<size_t>(depth) >= m_SortScratch.size())
{
m_SortScratch.emplace_back(); // deque: existing lists keep their addresses
}
std::vector<SortItem>& list = m_SortScratch[depth];
list.clear(); // keeps capacity, so no allocation once warmed up
return list;
}
void Standard2DRenderer::SortItems(std::vector<SortItem>& items)
{
// Lower order draws first; equal orders keep collection order.
std::sort(items.begin(), items.end(), [](const SortItem& a, const SortItem& b)
{ return a.order != b.order ? a.order < b.order : a.seq < b.seq; });
}
void Standard2DRenderer::CollectSortableItems(Deki::Object* obj, std::vector<SortItem>& items)
{
// Every object that reaches the sort is active and was reached through
// active parents (the walk starts at the scene roots), so RenderObject
// need not check again.
if (!obj || !obj->IsActive())
{
return;
}
const Renderables r = ResolveRenderables(obj);
// Built-in components first: a renderer, then any other sortable
// (Deki2D::ClipComponent, Deki2D::SortingGroupComponent, ...).
if (r.renderer)
{
items.push_back({ obj, r.renderer, r.clip, r.renderer->sortingOrder, static_cast<uint32_t>(items.size()) });
return;
}
if (r.sortable)
{
items.push_back({ obj, nullptr, r.clip, r.sortable->GetSortingOrder(), static_cast<uint32_t>(items.size()) });
return;
}
// Then the custom sorting callbacks.
int32_t order;
for (SortingCallback cb : m_SortingCallbacks)
{
if (cb(obj, order))
{
items.push_back({ obj, nullptr, r.clip, order, static_cast<uint32_t>(items.size()) });
return;
}
}
// Nothing claimed it: a transparent container, so its children are
// sorted at this level.
for (auto* child : obj->GetChildren())
{
CollectSortableItems(child, items);
}
}
// --- Main render loop ---
namespace
{
DrawView s_CurrentDrawView;
}
const DrawView& CurrentDrawView()
{
return s_CurrentDrawView;
}
void SetCurrentDrawView(const DrawView& view)
{
s_CurrentDrawView = view;
}
void Standard2DRenderer::Render(Deki::Scene* scene, const RenderContext& ctx)
{
if (!scene || !ctx.camera || !ctx.buffer)
{
return;
}
QuadBlit::ClearClipStack();
m_FrameBuffer = ctx.buffer;
// A package that loaded (or reloaded) since the last frame may have
// registered adapters for types already classified: start over.
const uint32_t adapterVersion = Deki::ComponentInterfaceAdapters::Version();
if (adapterVersion != m_TraitsVersion)
{
m_TypeTraits.clear();
m_TraitsVersion = adapterVersion;
}
RebuildHookLists();
// The frame's own context. Passes can swap frameCtx.buffer in BeginFrame
// to set the render target for the whole frame; every RenderObject below
// uses frameCtx, not the original ctx.
RenderContext frameCtx = ctx;
for (RenderPass* pass : m_BeginPasses)
{
pass->BeginFrame(frameCtx);
}
// Capture the camera once for the frame, against the target the passes
// settled on. Everything below maps world to screen through this. A strip
// captures it for the whole view, then moves it to the strip's pixels.
const int32_t viewWidth = frameCtx.viewWidth > 0 ? frameCtx.viewWidth : frameCtx.width;
const int32_t viewHeight = frameCtx.viewHeight > 0 ? frameCtx.viewHeight : frameCtx.height;
frameCtx.cam = frameCtx.camera->CaptureFrameCamera(viewWidth, viewHeight);
frameCtx.cam.halfW -= static_cast<float>(frameCtx.viewLeft);
frameCtx.cam.halfH -= static_cast<float>(frameCtx.viewTop);
SetCurrentDrawView({ frameCtx.cam.ppm, viewWidth, viewHeight });
// Content kept from earlier strips belongs to another frame now.
if (ctx.contentFrame == 0 || ctx.contentFrame != m_ContentCacheFrame)
{
m_ContentCache.clear();
m_ContentCacheFrame = ctx.contentFrame;
}
// Dirty-rect tracking: QuadBlit records every blit into the caller's
// buffer. A pass that installed its own frame target composites back into
// the caller's buffer itself, without QuadBlit, so that frame is fully
// dirty.
m_FrameDirtyValid = ctx.trackDirty;
if (ctx.trackDirty)
{
m_FrameDirty.Reset(ctx.width, ctx.height);
if (frameCtx.buffer != ctx.buffer || frameCtx.width != ctx.width || frameCtx.height != ctx.height)
{
m_FrameDirty.SetFull();
}
QuadBlit::SetDirtyTracking(&m_FrameDirty, ctx.buffer);
}
m_SortDepth = 0;
if (ctx.contentFrame != 0 && ctx.contentFrame == m_PlanFrame)
{
// A later strip of the frame: the order and geometry are recorded.
m_PlanMode = PlanMode::Replaying;
for (uint32_t i = 0; i < m_PlanRoots; i++)
{
RenderObject(m_Plan[i].item, frameCtx, static_cast<int32_t>(i));
}
}
else
{
// Collect and sort the root objects.
std::vector<SortItem>& sortableItems = SortListForDepth(0);
for (Deki::Object* obj : scene->GetObjects())
{
CollectSortableItems(obj, sortableItems);
}
// And the persistent objects.
const auto& persistentObjects = Deki::Engine::GetInstance().GetSceneSystem().GetPersistentObjects();
for (Deki::Object* obj : persistentObjects)
{
CollectSortableItems(obj, sortableItems);
}
SortItems(sortableItems);
// The first strip of a frame records it for the others: the roots
// first, each object's children after it as they are reached.
m_PlanMode = ctx.contentFrame != 0 ? PlanMode::Recording : PlanMode::Off;
m_Plan.clear();
m_PlanRoots = 0;
m_PlanFrame = 0;
m_PlanCovers = false;
if (m_PlanMode == PlanMode::Recording)
{
for (const SortItem& item : sortableItems)
{
PlanEntry entry;
entry.item = item;
m_Plan.push_back(entry);
}
m_PlanRoots = static_cast<uint32_t>(sortableItems.size());
}
// Render in sorted order. An index loop is safe: RenderObject recurses,
// but deeper levels use their own scratch lists, so this one stays put.
for (size_t i = 0; i < sortableItems.size(); i++)
{
RenderObject(sortableItems[i], frameCtx, m_PlanMode == PlanMode::Recording ? static_cast<int32_t>(i) : -1);
}
if (m_PlanMode == PlanMode::Recording)
{
m_PlanFrame = ctx.contentFrame;
}
}
m_PlanMode = PlanMode::Off;
// Post-frame composites (e.g. screen-space overlays).
for (auto it = m_EndPasses.rbegin(); it != m_EndPasses.rend(); ++it)
{
(*it)->EndFrame(frameCtx);
}
SetCurrentDrawView({});
if (ctx.trackDirty)
{
QuadBlit::SetDirtyTracking(nullptr, nullptr);
}
}
void Standard2DRenderer::RenderObject(const SortItem& item, const RenderContext& ctx, int32_t planIndex)
{
Deki::Object* obj = item.obj;
RenderContext objCtx = ctx;
// Phase 1: PreExecute passes, which may redirect ctx.buffer for this object.
for (RenderPass* pass : m_PrePasses)
{
pass->PreExecute(obj, objCtx);
}
// Phase 2: built-ins (sprite blit), into any target PreExecute redirected
// to. Recording, the entry's geometry is filled in; replaying, reused.
// (Taken by index here: m_Plan only grows below, after this call.)
ExecuteBuiltins(item, objCtx, planIndex >= 0 ? &m_Plan[planIndex].geo : nullptr);
// Phase 3: Execute passes (tilemap draw, etc.). Recording, each says
// whether it wants the object; replaying, only those that did run.
for (size_t i = 0; i < m_ExecPasses.size(); i++)
{
RenderPass* pass = m_ExecPasses[i];
if (i < 32)
{
const uint32_t bit = 1u << i;
if (m_PlanMode == PlanMode::Replaying && !(m_Plan[planIndex].execPasses & bit))
{
continue;
}
if (m_PlanMode == PlanMode::Recording && !pass->WantsObject(obj))
{
m_Plan[planIndex].execPasses &= ~bit;
continue;
}
}
pass->Execute(obj, objCtx);
}
// Phase 4: the sorted children, with objCtx, so they inherit any buffer
// redirect this object's PreExecute or Execute hooks applied.
if (m_PlanMode == PlanMode::Replaying)
{
// m_Plan does not change while replaying, so entries stay put.
const uint32_t first = m_Plan[planIndex].firstChild;
const uint32_t count = m_Plan[planIndex].childCount;
for (uint32_t i = 0; i < count; i++)
{
RenderObject(m_Plan[first + i].item, objCtx, static_cast<int32_t>(first + i));
}
}
else
{
++m_SortDepth;
std::vector<SortItem>& childItems = SortListForDepth(m_SortDepth);
for (auto* child : obj->GetChildren())
{
CollectSortableItems(child, childItems);
}
SortItems(childItems);
// Recording: the children's entries go in one block, and each child
// records its own children after it. Indices, not references: the
// plan grows as the recursion goes.
uint32_t first = 0;
if (m_PlanMode == PlanMode::Recording)
{
first = static_cast<uint32_t>(m_Plan.size());
for (const SortItem& child : childItems)
{
PlanEntry entry;
entry.item = child;
m_Plan.push_back(entry);
}
m_Plan[planIndex].firstChild = first;
m_Plan[planIndex].childCount = static_cast<uint32_t>(childItems.size());
}
for (size_t i = 0; i < childItems.size(); i++)
{
RenderObject(childItems[i], objCtx,
m_PlanMode == PlanMode::Recording ? static_cast<int32_t>(first + i) : -1);
}
--m_SortDepth;
}
// Phase 5: PostExecute passes, in reverse order.
for (auto it = m_PostPasses.rbegin(); it != m_PostPasses.rend(); ++it)
{
(*it)->PostExecute(obj, objCtx);
}
// Phase 6: built-ins again (clip pop).
PostExecuteBuiltins(item);
}
} // namespace DekiRendering