DEKI/ENGINE 文档
手册 API 参考
deki-rendering 0.40.1
Deki 包参考
deki-rendering

核心渲染管线:相机、渲染系统、标准 2D 渲染器、QuadBlit、渲染通道和排序。

仓库:github.com/dekiengine/deki-rendering · 最新版本:0.40.0 · 需要引擎 0.40.0 或更高版本

使用 DekiEditor --packages-add deki-rendering <project> 或通过包管理器安装。引擎自身的组件和 C++ API 见 Deki 文档。

在编辑器中显示为 **Rendering**。

组件

组件 类别 说明
CameraComponent 核心 视图:位置、显示的世界范围大小,以及清除颜色。
Light 渲染 光源:环境光、太阳、点周围或锥形。它照亮网格和 2D 世界。
ScreenEffect 渲染 作用于完成帧的全屏着色器,仅限显示通过 OpenGL 呈现时。

功能开关

可以从构建中去掉的部分。带有 define 的功能通过该 define 开关,其余的随其组件一起被剥离。

功能 define 默认值
Lights DEKI_FEATURE_LIGHTS 开

组件参考

CameraComponent

视图:位置、显示的世界范围大小,以及清除颜色。

检视器中的名称:相机组件 · 类别:核心 · 类型:DekiRendering::CameraComponent

属性 类型 默认值 约束 说明
clearColor Color "#314D79FF" 绘制任何内容之前用来填充屏幕的颜色。没有内容覆盖的地方就显示这个颜色。
projection enum "Orthographic" Orthographic, Perspective 正交以平面方式显示固定高度的世界范围。透视显示有纵深的视野,用于 3D 网格;精灵仍按平面绘制。
orthoHeight float 15 0.01 到 100000;单位 m;projection 允许时显示 相机从上到下显示的世界范围。每个屏幕都会缩放它以填满自身高度(旁边的 x)。
pixelPerfect bool false projection 允许时显示 用于像素画。只按整数倍缩放,使每个美术像素在屏幕上大小相同,并让相机和绘制的所有内容都落在整像素上。向下取整:相机显示的范围会比正交高度略多,而小于 1x 的屏幕会被裁剪。
pixelGrid enum "Screen" Screen, Art;projection 允许时显示 像素完美将相机和绘制的所有内容吸附到哪里。屏幕:整数屏幕像素,因此在放大显示美术的屏幕上移动也很平滑。美术:美术自身的像素,因此不会有东西落在两个像素之间,而在放大的屏幕上会按该大小一步步移动。
fieldOfView float 60 10 到 150;projection 允许时显示 垂直视野角度。屏幕越宽,两侧看到的越多。60 是常用默认值;越大视野越广,边缘畸变也越明显。
nearPlane float 0.1 projection 允许时显示 比此距离更近的内容不会绘制。调大它没有开销,还能提高深度精度,因此在场景允许的范围内尽量设大。
farPlane float 100 projection 允许时显示 比此距离更远的内容不会绘制。
clearEveryFrame bool true 每帧开始前清除屏幕。关闭时上一帧会保留在下方:当全屏背景覆盖一切时更快,有时也正好用于制作拖尾效果。
litUpToSortOrder int32 2147483647 光源(Light 组件)照亮此相机绘制的、排序顺序不超过此值的内容;绘制在其上的内容(如 UI)保持自己的颜色。默认照亮所有内容。

Light

光源:环境光、太阳、点周围或锥形。它照亮网格和 2D 世界。

类别:渲染 · 类型:DekiRendering::Light

属性 类型 默认值 约束 说明
type enum "Point" Ambient, Directional, Point, Spot 环境光对所有东西照亮程度相同。方向光像太阳一样从一个遥远的方向照射。点光源照亮物体周围直到范围。聚光灯沿物体的 X 轴照亮一个锥形,方向随其旋转。
color Color "#FFFFFFFF" 光的颜色。
intensity float 1 0 到 8 光的强度:1 在光最强处按颜色原样照亮,0.5 为一半。多个光源会叠加,最多为颜色的两倍。
range float 3 0.01 到 100000;单位 m;type 允许时显示 光照到达的距离(米)。到那里衰减为零。
spotAngle float 45 1 到 179;type 允许时显示 锥形的完整宽度(度)。
spotSoftness float 0.25 0 到 1(滑块);type 允许时显示 锥形向边缘衰减的程度:0 为硬边缘,1 从中心开始衰减。
yaw float -25 -180 到 180;type 允许时显示 光从哪里来,绕竖直轴旋转的角度(度)。0 在相机后方,90 在右侧。
pitch float 55 -90 到 90;type 允许时显示 光源的高度(度):90 为正上方向下,0 与地平线齐平。
bands int32 0 0 到 8;type 允许时显示 仅 2D:0 为平滑衰减。2 到 8 则以相应数量的清晰光环照亮,中间最亮,适合像素画。
castShadows bool false type 允许时显示 其光照范围内的网格会投射阴影,仅限显示通过 OpenGL 绘制时;每帧最多两个光源投射阴影。没有 OpenGL 的开发板会绘制光照但没有阴影,其构建会说明这一点。点光源暂不投射阴影。
shadowResolution int32 1024 128 到 4096;type 允许时显示 阴影贴图每边的像素数。越大越清晰,但会占用 GPU 内存和时间。
shadowBias float 0.002 0 到 0.1;type 允许时显示 把阴影从投射它的表面向后推。被照亮的表面上出现条纹状阴影时调高它;太高会让阴影与投射物脱开。

ScreenEffect

作用于完成帧的全屏着色器,仅限显示通过 OpenGL 呈现时。

检视器中的名称:屏幕效果 · 类别:渲染 · 类型:DekiGL::ScreenEffect

属性 类型 默认值 说明
shader AssetRef (Shader) "" 定义 float4 Effect(float2 uv) 的 HLSL .shader:返回窗口每个像素的颜色,uv 在整个帧上从左下到右上由 0 变到 1。Frame(uv) 读取帧。
params Vector3 [0.0, 0.0, 0.0] 着色器以 uParams 读取的三个数:强度、速度,或者它说明需要的任何东西。
color Color "#000000FF" 着色器以 uColor 读取的颜色。

项目设置

渲染 设置

项目设置分区 Rendering(RenderingProjectSettings)。

类别:设置 · 类型:DekiRendering::RenderingProjectSettings

属性 类型 默认值 约束 说明
frameMode enum "Auto" Auto, WholeFrames, Strips 每帧如何到达屏幕。Auto(默认)在游戏启动时于设备上选择:帧缓冲区能宽裕地放进内部 RAM 时用整帧,放不下时用条带(每像素 2 字节的 536x240 屏幕为 257 KB,超过 ESP32-S3 的内部 RAM 空余)。整帧在一个缓冲区中一次绘制每一帧,只要该缓冲区在内部 RAM 中就更快。条带每次在内部 RAM 的小缓冲区中绘制几行,并在绘制下一条的同时发送每一条,比在 PSRAM 中绘制整帧快得多。两者得到的像素相同,并保留所有功能。启动日志和 DEKI:STATS 会说明开发板使用哪一种以及原因。编辑器始终绘制整帧。
bandRows int32 40 2 到 256 以条带绘制帧时,每个条带的行数。40 适合大多数屏幕。条带越高,每帧遍历场景的次数越少,但需要更多内部 RAM:一个条带及其两个发送用副本,每个为屏幕宽度 × 此行数 × 每像素 2 字节。
redrawChanges bool false 只重绘变化的部分:每帧把每个对象绘制的内容与上一帧比较,只绘制并发送屏幕上不同的部分。什么都没动的帧几乎不花时间。屏幕大部分都在变化时(滚动的摄像机、全屏动画),该帧照常整帧绘制。如果有无法说明自己绘制位置的渲染通道,或有不清屏的摄像机,则每帧都整帧绘制。

更新日志

更新日志为英文。

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.

更早的版本:CHANGELOG.md


由包的反射数据生成(引擎 0.40.1)。属性名就是场景文件中使用的键。重命名后会保留别名(兼容性)。