Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDekiRenderSystem.cpp
More file actions
Latest commit
482 lines (437 loc) · 15.6 KB
/
Copy pathDekiRenderSystem.cpp
File metadata and controls
482 lines (437 loc) · 15.6 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
#include"DekiRenderSystem.h"
#include"DekiRenderer.h"
#include"DekiEngine.h"
#include"DekiLogSystem.h"
#include"SceneSystem.h"
#include"providers/DekiMemory.h"
#include"providers/IDekiDisplay.h"
#include"CameraComponent.h"
#include"DekiObject.h"
#include"Scene.h"
#include"RenderingProjectSettings.h"
#include"ProjectSettings.h"
#include"reflection/SettingsRegistry.h"
#include<algorithm>
#include<cstdint>
#include<cstdlib>
#include<cstring>
DekiRenderSystem::DekiRenderSystem()
: m_RenderBuffer(nullptr)
, m_ScreenWidth(0)
, m_ScreenHeight(0)
, m_ColorFormat(DekiColorFormat::RGB565)
{
}
DekiRenderSystem::~DekiRenderSystem()
{
if (m_RenderBuffer && m_OwnsBuffer)
{
DekiMemory::FreeInternal(m_RenderBuffer);
}
m_RenderBuffer = nullptr;
}
boolDekiRenderSystem::Setup(int32_t width, int32_t height, DekiColorFormat format)
{
// Project-wide rendering settings. In the editor the registry holds the
// hydrated instance; on device there is no registry, so the values come
// straight out of the loaded dproject.bin. Half-width and interlaced have
// no implementation yet and are only reported.
m_TrackDirty = false;
m_DirtyAlign = 32;
bool halfWidth = false, interlaced = false;
if (auto* rs = DekiSettingsRegistry::Instance().Get<RenderingProjectSettings>())
{
m_TrackDirty = rs->dirtyTileTracking;
m_DirtyAlign = rs->dirtyTileSize;
halfWidth = rs->halfWidthFramebuffer;
interlaced = rs->interlaced60hz;
}
else
{
// "Rendering" is RenderingProjectSettings' DEKI_PROJECT_SETTINGS_SECTION.
bool b = false;
int32_t a = 0;
if (ProjectSettings::ReadPackageSettingBool("Rendering", "dirtyTileTracking", b)) m_TrackDirty = b;
if (ProjectSettings::ReadPackageSettingInt32("Rendering", "dirtyTileSize", a)) m_DirtyAlign = a;
if (ProjectSettings::ReadPackageSettingBool("Rendering", "halfWidthFramebuffer", b)) halfWidth = b;
if (ProjectSettings::ReadPackageSettingBool("Rendering", "interlaced60hz", b)) interlaced = b;
}
if (m_DirtyAlign < 1) m_DirtyAlign = 1;
if (halfWidth || interlaced)
{
DEKI_LOG(LogLevel::Info, "[Rendering] settings: half_width=%d interlaced=%d (no implementation yet)",
(int)halfWidth, (int)interlaced);
}
if (m_TrackDirty)
DEKI_LOG_INTERNAL("[Rendering] dirty-rect tracking on, alignment %d px", m_DirtyAlign);
ResetDirtyHistory();
if (width <= 0 || height <= 0)
{
DEKI_LOG_ERROR("DekiRenderSystem::Setup: invalid size %dx%d", width, height);
returnfalse;
}
// Clean up existing buffers if any
if (m_RenderBuffer && m_OwnsBuffer)
{
DekiMemory::FreeInternal(m_RenderBuffer);
}
m_RenderBuffer = nullptr;
m_OwnsBuffer = true;
m_AdoptionCheckedDisplay = nullptr;
m_ScreenWidth = width;
m_ScreenHeight = height;
m_ColorFormat = format;
// Prefer a display-provided internal RAM buffer (avoids a memcpy in Present).
if (TryAdoptDisplayBuffer())
returntrue;
// No display yet, or its buffer does not match: own one. This used to
// "defer allocation until a display is available" and return true with a
// null buffer — but Render() only re-queried the display for non-owned
// buffers, so the allocation never happened and nothing was ever drawn,
// with no error. Now Setup either yields a usable buffer or says so.
int bytes_per_pixel = GetBytesPerPixel(format);
size_t buffer_size = (size_t)width * (size_t)height * (size_t)bytes_per_pixel;
m_RenderBuffer = (uint8_t*)DekiMemory::AllocateInternal(buffer_size, "DekiRenderSystem::Setup-framebuffer");
if (!m_RenderBuffer)
{
DEKI_LOG_ERROR("DekiRenderSystem::Setup: failed to allocate %zu-byte framebuffer (%dx%d)",
buffer_size, width, height);
returnfalse;
}
returntrue;
}
boolDekiRenderSystem::TryAdoptDisplayBuffer()
{
IDekiDisplay* display = DekiEngine::GetInstance().GetDisplay();
if (!display || display == m_AdoptionCheckedDisplay)
returnfalse;
m_AdoptionCheckedDisplay = display;
int32_t dw = 0, dh = 0;
uint8_t* directBuf = display->GetRenderBuffer(&dw, &dh);
if (!directBuf || dw != m_ScreenWidth || dh != m_ScreenHeight)
returnfalse;
if (m_RenderBuffer && m_OwnsBuffer)
DekiMemory::FreeInternal(m_RenderBuffer);
m_RenderBuffer = directBuf;
m_OwnsBuffer = false;
returntrue;
}
voidDekiRenderSystem::Render(Scene* current_scene)
{
if (!current_scene || !m_Renderer)
{
return;
}
// A display registered after Setup() may offer a direct buffer: adopt it
// once. Otherwise re-query the display buffer each frame for double-buffer
// support (render_index alternates in Present, so the pointer changes).
if (m_OwnsBuffer)
{
TryAdoptDisplayBuffer();
}
else
{
IDekiDisplay* display = DekiEngine::GetInstance().GetDisplay();
if (display)
{
int32_t dw = 0, dh = 0;
uint8_t* buf = display->GetRenderBuffer(&dw, &dh);
if (buf)
m_RenderBuffer = buf;
}
}
if (!m_RenderBuffer)
{
return;
}
// Find the scene's camera. Every frame, not cached: the cache used to be
// keyed on the Scene pointer, which a new scene at the same address (a
// tool rendering scenes in a loop) or a CameraComponent removed at
// runtime turned into a dangling component. The walk is a few hundred
// component-list checks against a frame of blits.
CameraComponent* camera = nullptr;
for (DekiObject* obj : current_scene->GetObjects())
{
DekiObject* holder = FindInSubtree(obj, [](DekiObject* o)
{ return o->GetComponent<CameraComponent>() != nullptr; });
if (holder)
{
camera = holder->GetComponent<CameraComponent>();
break;
}
}
if (!camera)
{
// Fall back to Persistent objects
constauto& persistentObjects = DekiEngine::GetInstance().GetSceneSystem().GetPersistentObjects();
for (DekiObject* obj : persistentObjects)
{
camera = obj->GetComponent<CameraComponent>();
if (camera) break;
}
}
// No camera = nothing to render
if (!camera)
{
return;
}
// ---- dirty-rect present -------------------------------------------------
// Anything the bookkeeping cannot vouch for (first use of a buffer, a
// size/format change, a clear-colour change, a frame the renderer could
// not describe, MarkAllDirty) is a full clear and a full present, so
// "off" and "unsure" both behave exactly as before.
constbool tracking = m_TrackDirty;
BufferHistory* hist = tracking ? &HistoryFor(m_RenderBuffer) : nullptr;
bool full = !tracking || m_ForceFull || !hist->valid;
const Deki::Color clear = camera->clearColor;
if (!m_HaveClearColor || clear.r != m_LastClearColor.r || clear.g != m_LastClearColor.g ||
clear.b != m_LastClearColor.b)
{
full = true;
m_LastClearColor = clear;
m_HaveClearColor = true;
}
// Clear before rendering, unless the camera says the scene paints every
// pixel itself. With tracking, only what the last frame on this buffer
// drew needs clearing.
if (camera->clearEveryFrame)
{
if (full || hist->lastDrawn.IsFull())
ClearBuffer(clear);
else
for (const DekiRect& r : hist->lastDrawn.Rects())
ClearRect(r.left, r.top, r.Width(), r.Height(), clear.r, clear.g, clear.b);
}
// Delegate to the active renderer
RenderContext ctx{camera, m_RenderBuffer, m_ScreenWidth, m_ScreenHeight, m_ColorFormat};
ctx.trackDirty = tracking;
m_Renderer->Render(current_scene, ctx);
if (!tracking)
{
m_PresentCount = -1;
return;
}
// What this frame drew, aligned so a small movement reuses its rectangle.
const DirtyRegion* drawn = m_Renderer->GetLastFrameDirty();
DirtyRegion& frame = m_DrawnScratch;
if (drawn)
{
frame = *drawn;
frame.Align(m_DirtyAlign);
}
else
{
frame.Reset(m_ScreenWidth, m_ScreenHeight);
frame.SetFull();
full = true;
}
// Present set: this frame's draws plus the previous frame's, whatever
// buffer that was rendered into.
if (full || frame.IsFull() || (m_HaveLastDrawn && m_LastDrawn.IsFull()))
{
m_PresentCount = -1;
}
else
{
m_PresentScratch = frame;
if (m_HaveLastDrawn)
m_PresentScratch.Union(m_LastDrawn);
if (m_PresentScratch.IsFull())
m_PresentCount = -1;
else
{
m_PresentRects = m_PresentScratch.Rects();
m_PresentCount = static_cast<int32_t>(m_PresentRects.size());
}
}
// History: what this buffer holds now, and what the screen is about to show.
hist->lastDrawn = frame;
hist->valid = true;
m_LastDrawn = frame;
m_HaveLastDrawn = true;
m_ForceFull = false;
}
DekiRenderSystem::BufferHistory& DekiRenderSystem::HistoryFor(constuint8_t* buffer)
{
for (BufferHistory& h : m_History)
if (h.buffer == buffer) return h;
m_History.push_back(BufferHistory{ buffer, DirtyRegion{}, false });
return m_History.back();
}
voidDekiRenderSystem::ResetDirtyHistory()
{
m_History.clear();
m_HaveLastDrawn = false;
m_HaveClearColor = false;
m_ForceFull = true;
m_PresentCount = -1;
}
voidDekiRenderSystem::SetDirtyTracking(bool enabled, int32_t alignment)
{
m_TrackDirty = enabled;
m_DirtyAlign = alignment < 1 ? 1 : alignment;
ResetDirtyHistory();
}
const DekiRect* DekiRenderSystem::GetPresentRects(int32_t* count) const
{
if (count) *count = m_PresentCount;
return m_PresentCount > 0 ? m_PresentRects.data() : nullptr;
}
voidDekiRenderSystem::RenderToBuffer(Scene* scene, ICamera* camera,
uint8_t* buffer, int32_t width, int32_t height,
DekiColorFormat format)
{
RenderToBufferStatic(scene, camera, buffer, width, height, format);
}
voidDekiRenderSystem::RenderToBufferStatic(Scene* scene, ICamera* camera,
uint8_t* buffer, int32_t width, int32_t height,
DekiColorFormat format)
{
if (!scene || !camera || !buffer)
return;
// Get the renderer from the engine's render system
DekiRenderer* renderer = DekiEngine::GetInstance().GetRenderSystem()->GetRenderer();
if (!renderer)
return;
// RenderContext uses CameraComponent* internally — safe cast since
// the rendering package owns CameraComponent and knows the concrete type
RenderContext ctx{static_cast<CameraComponent*>(camera), buffer, width, height, format};
renderer->Render(scene, ctx);
}
namespace
{
// One pixel of `format` at p; returns its size in bytes.
inlinesize_tWritePixel(uint8_t* p, DekiColorFormat format, uint8_t r, uint8_t g, uint8_t b)
{
switch (format)
{
case DekiColorFormat::RGB565:
{
constuint16_t v = static_cast<uint16_t>(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
memcpy(p, &v, 2);
return2;
}
case DekiColorFormat::RGB888:
p[0] = r; p[1] = g; p[2] = b;
return3;
case DekiColorFormat::ARGB8888:
{
constuint32_t v = (0xFFu << 24) | (static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | b;
memcpy(p, &v, 4);
return4;
}
case DekiColorFormat::RGB565A8:
{
constuint16_t v = static_cast<uint16_t>(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
p[0] = static_cast<uint8_t>(v & 0xFF);
p[1] = static_cast<uint8_t>(v >> 8);
p[2] = 0xFF; // opaque
return3;
}
}
return0;
}
} // namespace
voidDekiRenderSystem::ClearRect(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t r, uint8_t g, uint8_t b)
{
if (!m_RenderBuffer) return;
// Clip to the framebuffer.
int32_t x0 = std::max<int32_t>(x, 0), y0 = std::max<int32_t>(y, 0);
int32_t x1 = std::min<int32_t>(x + w, m_ScreenWidth), y1 = std::min<int32_t>(y + h, m_ScreenHeight);
if (x1 <= x0 || y1 <= y0) return;
constsize_t bpp = static_cast<size_t>(GetBytesPerPixel(m_ColorFormat));
constsize_t pitch = static_cast<size_t>(m_ScreenWidth) * bpp;
constsize_t span = static_cast<size_t>(x1 - x0) * bpp;
uint8_t* row0 = m_RenderBuffer + static_cast<size_t>(y0) * pitch + static_cast<size_t>(x0) * bpp;
// Seed one pixel, double it across the first row, then copy the row down:
// memcpy all the way instead of a per-pixel (or per-byte) loop.
WritePixel(row0, m_ColorFormat, r, g, b);
for (size_t written = bpp; written < span; written *= 2)
memcpy(row0 + written, row0, std::min(written, span - written));
for (int32_t yy = y0 + 1; yy < y1; ++yy)
memcpy(row0 + static_cast<size_t>(yy - y0) * pitch, row0, span);
}
voidDekiRenderSystem::ClearBuffer(uint8_t r, uint8_t g, uint8_t b)
{
ClearRect(0, 0, m_ScreenWidth, m_ScreenHeight, r, g, b);
}
voidDekiRenderSystem::ClearBuffer(const Deki::Color& color)
{
ClearBuffer(color.r, color.g, color.b);
}
DEKI_FAST_ATTRvoidDekiRenderSystem::GetPixel(int32_t x, int32_t y, uint8_t* r, uint8_t* g, uint8_t* b) const
{
if (!m_RenderBuffer || !r || !g || !b)
{
if (r) *r = 0;
if (g) *g = 0;
if (b) *b = 0;
return;
}
// Bounds check
if (x < 0 || x >= m_ScreenWidth || y < 0 || y >= m_ScreenHeight)
{
*r = *g = *b = 0;
return;
}
// Get pixel from render buffer based on format
switch (m_ColorFormat)
{
case DekiColorFormat::RGB565:
{
size_t pixel_index = (y * m_ScreenWidth + x) * 2;
uint16_t pixel = *((uint16_t*)(m_RenderBuffer + pixel_index));
*r = ((pixel >> 11) & 0x1F) << 3; // 5 bits -> 8 bits
*g = ((pixel >> 5) & 0x3F) << 2; // 6 bits -> 8 bits
*b = (pixel & 0x1F) << 3; // 5 bits -> 8 bits
break;
}
case DekiColorFormat::RGB888:
{
size_t pixel_index = (y * m_ScreenWidth + x) * 3;
*r = m_RenderBuffer[pixel_index];
*g = m_RenderBuffer[pixel_index + 1];
*b = m_RenderBuffer[pixel_index + 2];
break;
}
case DekiColorFormat::ARGB8888:
{
size_t pixel_index = (y * m_ScreenWidth + x) * 4;
uint32_t pixel = *((uint32_t*)(m_RenderBuffer + pixel_index));
*r = (pixel >> 16) & 0xFF;
*g = (pixel >> 8) & 0xFF;
*b = pixel & 0xFF;
break;
}
case DekiColorFormat::RGB565A8:
{
size_t pixel_index = (y * m_ScreenWidth + x) * 3;
uint16_t pixel = *((uint16_t*)(m_RenderBuffer + pixel_index));
*r = ((pixel >> 11) & 0x1F) << 3;
*g = ((pixel >> 5) & 0x3F) << 2;
*b = (pixel & 0x1F) << 3;
break;
}
}
}
DEKI_FAST_ATTR Deki::Color DekiRenderSystem::GetPixel(int32_t x, int32_t y) const
{
uint8_t r, g, b;
GetPixel(x, y, &r, &g, &b);
returnDeki::Color(r, g, b);
}
intDekiRenderSystem::GetBytesPerPixel(DekiColorFormat format)
{
switch (format)
{
case DekiColorFormat::RGB565:
return2;
case DekiColorFormat::RGB888:
return3;
case DekiColorFormat::ARGB8888:
return4;
case DekiColorFormat::RGB565A8:
return3;
}
return2;
}