Repository navigation
Expand file tree
/
Copy pathStandard2DRenderer.cpp
More file actions
491 lines (428 loc) · 16.7 KB
/
Copy pathStandard2DRenderer.cpp
File metadata and controls
491 lines (428 loc) · 16.7 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
#include "Standard2DRenderer.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);
}
}
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)
{
Deki::Object* obj = item.obj;
const Deki::WorldTransform wt = obj->GetWorldTransform(); // one dirty check for all five values
// Clip: push a clip rect if the object has an IClipProvider.
if (item.clip)
{
float fScreenX, fScreenY;
ctx.cam.WorldToScreen(wt.x, wt.y, fScreenX, fScreenY);
int32_t screenX = static_cast<int32_t>(std::floor(fScreenX));
int32_t screenY = static_cast<int32_t>(std::floor(fScreenY));
const float effective = ctx.cam.ppm;
float scaledW = item.clip->GetClipWidth() * effective * wt.scaleX;
float scaledH = item.clip->GetClipHeight() * effective * wt.scaleY;
int32_t left = screenX - static_cast<int32_t>(std::floor(scaledW * 0.5f));
int32_t top = screenY - static_cast<int32_t>(std::floor(scaledH * 0.5f));
QuadBlit::PushClipRect(left, top, left + static_cast<int32_t>(scaledW), top + static_cast<int32_t>(scaledH));
}
// Renderer (a sprite, say): blit its content.
RendererComponent* renderer = item.renderer;
if (renderer)
{
const bool useOrderedDither = (renderer->alphaMode == AlphaMode::OrderedDither);
float fScreenX, fScreenY;
ctx.cam.WorldToScreen(wt.x, wt.y, fScreenX, fScreenY);
// Cull before RenderContent, which may rasterise text, bake a gradient
// or copy a frame. The screen 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;
if (renderer->GetContentExtents(extentW, extentH))
{
const float cx = fScreenX, cy = fScreenY;
const float reach = (std::fabs(extentW * wt.scaleX) + std::fabs(extentH * wt.scaleY)) * ctx.cam.ppm + 2.0f;
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;
if (renderer->RenderContent(obj, source, pivotX, pivotY, tintR, tintG, tintB, tintA))
{
// 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 = ctx.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 (ctx.cam.snapStep > 0)
{
fScreenX = ctx.cam.SnapX(fScreenX);
fScreenY = ctx.cam.SnapY(fScreenY);
}
const int32_t intScreenX = (renderer->pixelSnap || ctx.cam.snapStep > 0)
? static_cast<int32_t>(std::lround(fScreenX))
: static_cast<int32_t>(fScreenX);
const int32_t intScreenY = (renderer->pixelSnap || ctx.cam.snapStep > 0)
? static_cast<int32_t>(std::lround(fScreenY))
: static_cast<int32_t>(fScreenY);
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 (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();
// 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.
frameCtx.cam = frameCtx.camera->CaptureFrameCamera(frameCtx.width, frameCtx.height);
SetCurrentDrawView({ frameCtx.cam.ppm, frameCtx.width, frameCtx.height });
// 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);
}
// Collect and sort the root objects.
m_SortDepth = 0;
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);
// 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);
}
// 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)
{
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.
ExecuteBuiltins(item, objCtx);
// Phase 3: Execute passes (tilemap draw, etc.).
for (RenderPass* pass : m_ExecPasses)
{
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.
++m_SortDepth;
std::vector<SortItem>& childItems = SortListForDepth(m_SortDepth);
for (auto* child : obj->GetChildren())
{
CollectSortableItems(child, childItems);
}
SortItems(childItems);
for (size_t i = 0; i < childItems.size(); i++)
{
RenderObject(childItems[i], objCtx);
}
--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