Core rendering system with camera and renderer components
Repository: github.com/dekiengine/deki-rendering · Latest release: 0.40.0 · Needs engine 0.40.0+
Install with DekiEditor --packages-add deki-rendering <project>, or from the Package Manager. The engine's own components and C++ API are in the Deki docs.
Shown in the editor as Rendering.
Components
| Component | Category | Description |
| CameraComponent | Core | The view: position, how much world it shows, and clear color. |
| Light | Rendering | A light: ambient, the sun, round a point or in a cone. It lights meshes and the 2D world. |
| ScreenEffect | Rendering | A full-screen shader over the finished frame, where the display presents through OpenGL. |
Feature flags
Parts you can leave out of a build. A feature with a define is switched there; the rest are stripped with their components.
| Feature | Define | Default |
| Lights | DEKI_FEATURE_LIGHTS | on |
Component reference
CameraComponent
The view: position, how much world it shows, and clear color.
Inspector name: Camera Component · Category: Core · Type: DekiRendering::CameraComponent
| Property | Type | Default | Constraints | Description |
| clearColor | Color | "#314D79FF" | | Colour the screen is filled with before anything is drawn. What shows wherever nothing covers it. |
| projection | enum | "Orthographic" | Orthographic, Perspective | Orthographic shows a fixed height of world, flat. Perspective shows a field of view in depth, for 3D meshes; sprites still draw flat. |
| orthoHeight | float | 15 | 0.01 to 100000; in m; shown when projection allows | How much world the camera shows top to bottom. Every screen scales it to fill its height (the x beside it). |
| pixelPerfect | bool | false | shown when projection allows | For pixel art. Scales by whole numbers only, so every art pixel is the same size on screen, and keeps the camera and everything drawn on whole pixels. Rounds down: the camera shows a little more than its ortho height, and a screen too small for 1x crops. |
| pixelGrid | enum | "Screen" | Screen, Art; shown when projection allows | What Pixel Perfect snaps the camera and everything drawn to. Screen: whole screen pixels, so movement is smooth on a screen that shows the art larger. Art: the art's own pixels, so nothing sits between two of them, and on a larger screen things move in steps of that size. |
| fieldOfView | float | 60 | 10 to 150; shown when projection allows | Vertical field of view in degrees. A wider screen sees more at the sides. 60 is a common default; larger looks wider and more distorted at the edges. |
| nearPlane | float | 0.1 | shown when projection allows | Nothing closer than this is drawn. Raising it costs nothing and buys depth precision, so keep it as large as the scene allows. |
| farPlane | float | 100 | shown when projection allows | Nothing further than this is drawn. |
| clearEveryFrame | bool | true | | Clear before each frame. Off, the previous frame stays underneath: faster when a full-screen background covers everything, and occasionally what you want for trails. |
| litUpToSortOrder | int32 | 2147483647 | | Lights (the Light component) light what this camera draws up to this sort order; what draws above it, such as the UI, keeps its own colours. The default lights everything. |
Light
A light: ambient, the sun, round a point or in a cone. It lights meshes and the 2D world.
Category: Rendering · Type: DekiRendering::Light
| Property | Type | Default | Constraints | Description |
| type | enum | "Point" | Ambient, Directional, Point, Spot | Ambient lights everything the same. Directional lights from one far direction, like the sun. Point lights round the object, out to the range. Spot lights a cone along the object's X axis, the way its rotation turns it. |
| color | Color | "#FFFFFFFF" | | The light's colour. |
| intensity | float | 1 | 0 to 8 | How strong the light is: 1 lights a colour as it is where the light is strongest, 0.5 half as much. Lights add up, to at most twice a colour. |
| range | float | 3 | 0.01 to 100000; in m; shown when type allows | How far the light reaches, in meters. It fades to nothing there. |
| spotAngle | float | 45 | 1 to 179; shown when type allows | The cone's full width, in degrees. |
| spotSoftness | float | 0.25 | 0 to 1 (slider); shown when type allows | How much of the cone fades out toward its edge: 0 is a hard edge, 1 fades from the centre. |
| yaw | float | -25 | -180 to 180; shown when type allows | Where the light comes from, swept around the vertical axis, in degrees. 0 puts it behind the camera, 90 to the right. |
| pitch | float | 55 | -90 to 90; shown when type allows | How high the light is, in degrees: 90 straight down, 0 level with the horizon. |
| bands | int32 | 0 | 0 to 8; shown when type allows | 2D only: 0 fades smoothly. 2 to 8 lights in that many hard rings, the brightest in the middle, for pixel art. |
| castShadows | bool | false | shown when type allows | Meshes in its light cast shadows, where the display draws through OpenGL; at most two lights in a frame cast them. A board without OpenGL draws the light without its shadows, and its build says so. Point lights cast none yet. |
| shadowResolution | int32 | 1024 | 128 to 4096; shown when type allows | Pixels on each side of the shadow map. More is sharper and costs GPU memory and time. |
| shadowBias | float | 0.002 | 0 to 0.1; shown when type allows | Pushes shadows back from the surfaces that cast them. Raise it when a lit surface shows stripes of shadow on itself; too much lets shadows come loose from their casters. |
ScreenEffect
A full-screen shader over the finished frame, where the display presents through OpenGL.
Inspector name: Screen Effect · Category: Rendering · Type: DekiGL::ScreenEffect
| Property | Type | Default | Description |
| shader | AssetRef (Shader) | "" | An HLSL .shader that defines float4 Effect(float2 uv): the colour of each window pixel, where uv runs 0 to 1 over the frame, bottom left to top right. Frame(uv) reads the frame. |
| params | Vector3 | [0.0, 0.0, 0.0] | Three numbers the shader reads as uParams: strengths, speeds, whatever it says it wants. |
| color | Color | "#000000FF" | A colour the shader reads as uColor. |
Project settings
Rendering settings
Project settings section Rendering (RenderingProjectSettings).
Category: Settings · Type: DekiRendering::RenderingProjectSettings
| Property | Type | Default | Constraints | Description |
| frameMode | enum | "Auto" | Auto, WholeFrames, Strips | How each frame reaches the screen. Auto (the default) chooses on the device when the game starts: whole frames when the framebuffer fits in internal RAM with room to spare, strips when it does not (a 536x240 screen at 2 bytes a pixel is 257 KB, more than the free internal RAM of an ESP32-S3). Whole frames draws each frame once, in one buffer, and is the faster choice whenever that buffer is in internal RAM. Strips draws the frame a strip of rows at a time into a small buffer in internal RAM and sends each strip while the next is drawn, which is much faster than drawing a whole frame in PSRAM. Both give the same pixels and keep every feature. The boot log and DEKI:STATS say which one a board uses and why. The editor always draws whole frames. |
| bandRows | int32 | 40 | 2 to 256 | Rows in each strip, when frames are drawn in strips. 40 suits most screens. Taller strips walk the scene fewer times a frame but need more internal RAM: a strip, and two copies of it for sending, of the screen's width times this many rows at 2 bytes a pixel. |
| redrawChanges | bool | false | | Redraw only what changed: each frame compares what every object draws with the last frame and draws and sends only the parts of the screen that differ. A frame where nothing moved costs almost nothing. When most of the screen changes (a scrolling camera, a full-screen animation) that frame is drawn whole as usual. A render pass that cannot say where it draws, or a camera that does not clear, makes every frame whole. |
Changelog
0.40.0
Added
- Pixel Grid on the camera: what Pixel Perfect snaps to. Screen, the default, keeps the camera and everything drawn on whole screen pixels. On a screen that shows the art larger the camera then moves a screen pixel at a time: a 2:1 isometric walk at 2x moves the map 2 pixels across and 1 down every frame, where it moved 2 down every other frame. Art is the grid Pixel Perfect had: the art's own pixels, so nothing sits between two of them. At 1x the two are the same. FrameCamera::snapStep is 1 for Screen.
- Blits onto ARGB8888 at a whole-number scale take a fast path. Every fast path was for RGB565 screens, so on an ARGB8888 one every sprite and tile, even at 1:1, was sampled pixel by pixel by the generic pipeline. A plain blit (no tint, key, dither or flips) at 1x, 2x, 3x and so on now reads each source pixel once and fills the block it covers (QuadBlit::WholeScaleRows_ARGB8888), with the pipeline's pixels. Nothing to set. On the Anbernic RG40XX H (640x480, a pixel-art farm drawn at 2x) blits went from 12.3 to 6.4 ns a pixel and the game from 35-47 to 60 frames a second.
- A renderer can draw many pieces. RendererComponent::GetPieceCount, GetPiecePlace, GetPieceExtents, RenderPieceContent, GetPieceVersion and GetPieceDrawKey: each piece is placed and sorted on its own, as a child object would be, and read from the one component. A renderer with no pieces (the default) draws as before. deki-2d's SpriteBatchComponent uses it.
- Sprites are drawn from runs. A sprite drawn 1:1 and unturned onto RGB565 keeps its box of non-transparent pixels as runs: for each row, how many pixels to pass over, how many to copy and how many to blend, with only those pixels kept (QuadBlit::PackRuns, BlitRuns, PackedPixels). Drawn from the picture, every pixel of an animation frame was read, three bytes each, to find the ones to draw; where pictures are in external memory that reading was most of what a character cost. A sprite mirrored across or down has runs of its own, and a tint or a fade is applied as the runs are drawn, so it can change every frame. Nothing to set: the runs are made the first time a sprite of up to 128x128 pixels is drawn, on targets that have external memory, in 64 KB blocks of it taken as they fill: up to eight, and never the last half megabyte the target has free. A character's frame takes about 1.2 KB. A sprite scaled, turned or dithered, or one there is no room for, is drawn as before, and another scene starts with the store empty. The pixels are the same, checked against the plain blit over random pictures, mirrors, tints, positions and clips, and on a board frame by frame. On a LilyGO T-Deck the "Your
first game" farm with 16 villagers walking went from 18.6 to 16.7 ms a frame, the display's 60 fps, as with none; it holds 16.9 ms with each villager on a sheet of its own (256 frames, about 300 KB of runs), and 17 ms with half of them mirrored, a quarter tinted and a quarter faded.- The profiler breaks a frame down by object. While it is on, each renderer, and each render pass on each object, has a row (RenderProfiler::TakeObjects): its path and type, its share of finding what changed, of making and of drawing its pixels, how many times it was drawn, and why its area was drawn again (new, changed, still on screen while the world scrolled, drawn over a pin). Off, nothing is counted. The Render Profiler window (Tools > Rendering) shows the rows for the Play view, or for a board on a serial port. Render passes name themselves for it (RenderPass::Name, "Render pass" unless they say otherwise).
- Redraw Changes moves the last frame when the camera scrolls. A camera that moved by whole pixels, and sits on whole pixels, used to redraw the whole frame; now the frame's pixels are moved and only the edge it uncovers and what changed besides are drawn. Frames match ones drawn whole. A scene with 2D lights, a frame drawn in strips, a camera between pixels, and a render pass that reports its own changes but not RenderPass::FollowsScroll still redraw whole frames. DekiRenderer::ScrolledBy and RenderContext::scrollable, scrollX and scrollY carry it; a custom renderer needs no change. New virtuals on DekiRenderer and RenderPass: packages built against the old headers must be rebuilt.
- What stays on screen keeps its pixels while the camera scrolls. A renderer drawn the same at the same place on screen, such as a HUD panel under the camera, keeps the pixels of its opaque middle as the frame moves (DekiRenderer::ScrollPins); only its see-through edge and the band it leaves are drawn again. Anything drawn over it that moved redraws it.
- A drawing's area is only what it can write. A sprite, shape or other renderer drawn 1:1 and unturned counts only the box of its non-transparent pixels as changed: an animation frame's empty margin is not redrawn. QuadBlit::VisibleBounds and QuadBlit::OpaqueInterior work them out; they are kept, by pixels and revision, in a small table in external memory. Pixels with no Source::revision are not trimmed.
- Changed parts close together are drawn as one. Each part redrawn walks the scene and sets up every render pass again, so parts are merged while a merge adds fewer pixels than that costs (DirtyRegion::MergeCheaperThan, DekiRenderSystem::SetPartCostPixels, 4096 by default).
- DeferredClear::colour and QuadBlit::PendingClearColour: the colour of a clear still pending, whatever it covers, for a render pass about to write every pixel the clear covers in a frame or a redrawn part of one, which then drops the clear (DeferredClear::pixel stays for whole frames only). DekiRenderSystem and the lit clear of 2D lights give it.
- QuadBlit::SwapClipState sets the clip stack, the target limit and the clip switch aside, for a pass drawing into a buffer of its own in the middle of a frame.
- Finding what changed reuses what it measured. A renderer can give a draw key (RendererComponent::GetDrawKey): a value from its own fields that changes whenever its content could. While the key, the object's scale and its turn stay the same, what was measured of its content (its size, pixels, area and the signature they make) is reused, and only where it is drawn is worked out again; a renderer with no key is measured in full each frame as before. Kept in a table in external memory. A new virtual on RendererComponent: packages built against the old headers must be rebuilt. On a LilyGO T-Deck, finding the changes of the "Your first game" farm went from 7.6 to 6.2 ms a frame.
Changed
- minEngine 0.40.0: built against engine 0.40.0. A build of this package from before does not load and is rebuilt.
- A frame redrawn in parts costs less with many sprites moving. On a LilyGO T-Deck, the "Your first game" farm with the camera following the farmer and villagers walking about went from 18.1 to 16.6 ms a frame with 4 villagers, 20.6 to 16.7 ms with 8 (both now the display's 60 fps) and 26.3 to 18.6 ms with 16. The pixels are the same: checked on the board, each frame drawn in parts against the same frame drawn whole. What changed:
- A sprite drawn 1:1 and unturned blits only its box of non-transparent pixels, as only that box already counted as its area. An animation frame's empty margin is no longer read.
- What is known of a source's pixels (its visible box, its opaque interior) is kept in a few places for each source, not one: sources that shared a place pushed each other out every frame, and each time the pixels were walked again.
- Each part passes over the objects outside it from a short list of their areas, made as the frame's plan is built, without reading the plan.
- A renderer's content is kept beside what is reused of it from frame to frame, not copied into the plan for every object every frame.
- DirtyRegion keeps the sum of its areas as rectangles are added, and merging looks again only at the pairs a merge changed. The pairs merged are the same (new tests compare them with comparing every pair again).
- A scrolled frame's rows find the pins they cross only where a pin starts or ends.
- DirtyRegion::Add joins a rectangle to one already there when their bounding box is no larger than the two together: a sprite that moved a pixel is one rectangle, not two. Above the merge threshold the set is merged down to a quarter under it, not to it, so the next rectangles are added without merging again.
- Parts are drawn as one when closer than 2048 pixels of extra area (kPartCostPixels, was 4096): a part costs less than it did.
- Changed parts are rounded out to 4 pixels, not 8 (kRedrawAlign): fewer pixels are drawn again around a small sprite. A display rounds what it is sent for itself.
- The frame's plan and its lists are in external memory (ExternalAllocator) where the target has it. They came from the C library, which on a board put them, or the sizes they grew through, in internal RAM.- Redrawn parts cost less. Taking what stays on screen out of what changed allocates nothing (1.85 to 0.2 ms a frame on the T-Deck farm), and a part passes over an object outside it that has nothing else to draw before setting anything up for it.
- Finding what changed no longer allocates every frame. DirtyRegion::Align folded its rectangles back through a new list each time and threw the old one away, so the region grew its list again from nothing: five allocations a frame on a T-Deck. It keeps both lists now.
Breaking
- Positive rotation now draws counter-clockwise. QuadBlit applied the angle in screen space, where Y runs down, so a sprite turned clockwise while its children, lights, 3D and physics turned counter-clockwise. A rotated sprite now turns the same way as the rest of the engine, and a child stays fixed to a rotated parent. A scene or script that set a rotation by eye needs the sign of that rotation flipped.
- A turned sprite no longer moves by a pixel. QuadBlit read each pixel of a rotated draw at its corner instead of its centre, so a sprite turned by a half turn or a quarter turn landed one pixel right of, or below, where it stands unturned. It now covers the same pixels, as Tiled and other editors draw it. Rotated sprites placed by eye may sit a pixel off.
- A scaled sprite reads the source pixel under each drawn pixel's middle. QuadBlit::BlitScaled read the one under its left or top edge. A sprite drawn at a whole multiple of its size looks the same; one shrunk, or grown by a fraction, shows some different edge pixels: a 6-pixel strip drawn 2 wide now shows its second and fifth pixels, not its first and fourth. Exactly between two pixels it takes the first, so halving still takes every other pixel from the first. This matches turned sprites and Tiled. The cost is the same.
Fixed
- The profiler's kinds of component kept no name for draws whose content was reused, so a board's DEKI:PROFILE listed them as "?": AddBlit takes the name too.
0.30.0
Declares the capabilities it uses
- package.json has "capabilityDefines": ["opengl"] and "filtersTexturesWith": ["opengl"]: its GPU code tests DEKI_CAPABILITY_OPENGL, and its GL sprite sink draws textures filtered. The editor reads them instead of knowing opengl itself; needs the editor from this release.
Changed
- minEngine 0.30.0: built against engine 0.30.0. A build of this package from before does not load and is rebuilt.
- Lights are a feature (lights, DEKI_FEATURE_LIGHTS, the Light component): a device build whose scenes and code have no Light leaves it out. Light.cpp is not compiled, nothing is lit, and the light loops stay in flash: such a game spends no internal RAM on lights (a T-Deck build had 1.4 KB of their code there, and 4.2 KB of tables, below). Code that uses Light includes Light.h inside #ifdef DEKI_FEATURE_LIGHTS; RenderingFeatures.h turns it on in a build that does not configure features.
- The 2D light's colour tables (about 2 KB for each thread that lights) are allocated by the first frame lit, not held static: a game without lights keeps that internal RAM (4.2 KB on a T-Deck, where a second core lights too). With no memory for them the frame is drawn unlit, and the log says so once.
- The frame's clear waits for its first write, wherever that is: the renderer leaves it with QuadBlit (SetPendingClear), a blit into the buffer runs it first, and so must a render pass that writes into the buffer itself and says it does not (RenderPass::WritesBuffer false): it now calls QuadBlit::BeforeWriting(ctx.buffer) before it writes, as well as NoteWritten after. Such a pass that does not would have its pixels cleared over. Before, any pass with a hook had the clear made first.
- A frame holding nothing but its clear when the 2D light is due, and lit the same all over (Lighting2D::Uniform: no point or spot light in view), is cleared in the clear colour lit (DeferredClear::runLit) instead of cleared and lit after, still when the frame is first written. The same pixels. A 3D scene lit by an ambient light and the sun saves the 2D light's pass over the screen: 6.8 ms a frame on a T-Deck.
- A frame that cannot be drawn in strips (a 3D scene's) is drawn in a frame the display lends (IScanOrderedDisplay::LendFrame) when every pixel of it is drawn anew (the camera clears every frame, changes are not redrawn on their own, no screenshot is wanted), and goes to the display as it is. On a T-Deck that is the 5 ms copy of each frame gone: a 3D corridor of 2,000 triangles at 19.5 frames a second, from 17.
- A render pass about to write every pixel of the frame itself can take the pending clear as one pixel (QuadBlit::TakePendingClear, DeferredClear::pixel), lit when the light came first, and draw it where it draws nothing: deki-3d's software rasteriser does, which saves the clear's own pass over the screen.
- The shader loader is registered once, as "Shader" (ShaderAsset::kAssetTypeName). Its second name, "ShaderAsset", was there because the reflection named an AssetRef<ShaderAsset> by its class; it no longer does (engine changelog).
Added
- GL::GenerateMipmap, gl::LINEAR_MIPMAP_LINEAR and gl::NEAREST_MIPMAP_NEAREST, for filtered textures.
- A source's filter (QuadBlit::Source::filter: Point, Bilinear, Trilinear, the image's setting). GLBlitSink draws Bilinear blended between the four nearest texels, and Trilinear also between mipmaps by how small it is drawn, kept within each source so a frame or tile never takes in its neighbour's texels; a texture gets its mipmaps when a Trilinear source first draws from it. The CPU draws Point, as before.
- Lights: the Light component (Ambient, Directional, Point, Spot; colour, intensity, range, a spot's angle and softness, a sun's yaw and pitch, 2D bands, shadows). Every active Light in the scene lights deki-3d's meshes and the 2D world; a scene with none is lit as before, not at all.
- The 2D light, Lighting2D: worked out per 4x4 block, blended and dithered between blocks where it changes, up to twice a colour, laid over the frame by Standard2DRenderer before the first object above the camera's litUpToSortOrder (new; the default lights everything) or before a renderer that LightsItself() (new, on RendererComponent). Strips and redraws of part of the frame are lit as the whole frame, and a changed light is a changed region. On a device both cores light the pixels.
- On the GPU the 2D light is a full-screen pass, per pixel (QuadBlit::BlitSink::Light, QuadBlit::LightSinkFrame, the presenter's 2D light program, HLSL in editor/Light2DShader.cpp, compiled into GLLight2DShader.inl). A sink that cannot light says so, once.
- In the editor: the Light inspector and its gizmos (a point's reach, a spot's cone), and a check of the scenes a build ships that lists each shadow-casting light a board without OpenGL draws without its shadows.
- OpenGL, where the platform provides opengl (its build then defines DEKI_CAPABILITY_OPENGL; a board never does and compiles none of it): the GL loader and shared context (GLApi.h), a presenter that builds each frame on the GPU (GLPresenter), the 2D drawn there too (GLBlitSink), and ScreenEffect, a full-screen shader over the finished frame. Types live in DekiGL.
- ShaderAsset (.shader): a GPU shader, written in HLSL. The editor compiles it (Slang, then SPIRV-Cross) to GLSL ES 3.00 and GLSL 3.30 for each use whose hooks it defines (editor/ShaderUses.h: deki-3d's meshes, screen effects), and the asset is that GLSL. Errors name the file and line, in the Console and in a build's problems; a build for a target without OpenGL that ships a scene using a shader stops. The editor lists it as the Shader asset type.
- A blit sink (QuadBlit::BlitSink), so a GPU that builds the frame (GLPresenter) can draw its 2D. While a sink frame is on (QuadBlit::BeginSinkFrame), every Blit and BlitScaled into that frame's buffer goes to the sink, worked out as the CPU would draw it (SinkBlit: the clipped rectangle and the 16.16 steps it samples with), and nothing reaches the buffer. Without a sink a blit tests one pointer more and nothing else changes.
- DekiRenderSystem draws the whole frames of a camera that clears into a sink that takes them: the clear is the sink's, and the buffer starts empty (transparent) and collects only what code writes into it directly. Redrawing only what changed is skipped then, as the sink's frame starts over each time. Strips and a camera that keeps its frame stay on the CPU.
- RenderPass::WritesBuffer: whether a pass writes into the buffer other than through QuadBlit. On a sink frame the renderer tells the sink after each hook of a pass that does (QuadBlit::NoteWritten), so what it wrote is composed in its place in the draw order. Yes by default; a pass that draws only through QuadBlit says no and saves a look through the buffer.
- QuadBlit::Source::revision and QuadBlit::NextRevision(): a number for a source's pixels, new whenever they are written, so a sink keeps its upload until it changes. ForgetScaled tells the sink too.
Changed
- QuadBlit's row loops stay where they are meant to on an ESP32. Each one Blit or BlitScaled calls is in IRAM and never inlined into those callers, which run from flash; before, the compiler inlined some of them and dropped their IRAM copy, and which ones changed with any edit nearby (the 1:1 alpha blend of RGB565A8 sprites ran from flash). The general per-pixel path (BlitRows) is in flash, as it was in every build: in IRAM it took 12 KB and drew Wyrdmoor on a T-Deck no faster. IRAM on a T-Deck build: 2.4 KB more.
Earlier releases: CHANGELOG.md.
Generated from the package's reflection data (engine 0.40.1). Property names are the keys used in scene files. Renames keep an alias (compatibility).