Mass Battle Frameworkv1.20.0

逻辑在 CPU,画在 GPU Logic on the CPU, Pictures on the GPU

RENDERING

渲染管线的分界线 · 2026-11-21Where the render pipeline draws its line · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

一条分界线 One Line

整套渲染管线可以压成一条线:逻辑在 CPU 上算,画面在 GPU 上画。这条线是设计前提,性能和可调性都是它带来的结果。下面说两边的理由,再说越过这条线要付什么。

The whole render pipeline comes down to one line: logic runs on the CPU, pictures are drawn on the GPU. The line came first, and the performance and the adjustability follow from it. Below: why each side sits where it does, and what crossing the line costs.

为什么逻辑必须在 CPU Why Logic Belongs on the CPU

第一条理由很朴素:写了就生效,读了就有值。在 CPU 上,单位的数据就地改,下一行就能读回来;不用把数据搬去别处再搬回来,也不用为了知道"什么时候能读"去绑回调、挂委托。开发时这个差别很大。调试一个行为,你可以在任何一帧停下来,把它的字段一个个打出来看。

The first reason is plain: write it and it takes effect, read it and it is there. On the CPU you change a unit's data in place and read it back on the next line. Nothing gets shipped off and back, and there is no callback or delegate guarding the answer to "when can I read this". In development that counts for a lot: to debug a behaviour you stop on any frame and print the fields.

第二条理由是决定性的:确定性。同样的输入、同样的 tick,每个端要得出逐位相同的结果,模拟就只能待在 CPU 上。GPU 的执行顺序、精度、调度都不归你管,回读也不便宜、不可靠。模拟不定,大规模单位的联机同步就无从谈起:两端一旦算出不一样的结果,你只能一直纠正,而且越纠越贵。

The second reason is decisive: determinism. Same input, same tick, bit-identical results on every peer. That only works if the simulation runs on the CPU. GPU execution order, precision and scheduling are not yours to control, and readback is neither cheap nor dependable. Without a deterministic simulation there is no large-scale networked play: once two peers compute different results you are stuck correcting them, and the corrections get more expensive the longer the battle runs.

所以逻辑层不用为画面做任何妥协:字段随便读写,它是唯一的事实来源。

So the logic layer makes no concessions to drawing. It reads and writes whatever fields it likes; it is the only source of truth.

为什么画面交给 Niagara,而不是简单实例化 Why Niagara, Not Plain Instancing

画面用一个实例化的静态网格组件就能画完,那是最省事的做法。我们没走那条路,图的是Niagara 能在 GPU 上做计算,能把一部分压在 CPU 上的活分走。CPU 省下的时间拿去喂模拟:多装一些单位,或者同样的单位多留一点余量。

An instanced static mesh component could draw the pictures, and it would be the easy route. We went a different way because Niagara computes on the GPU and can take part of the load off the CPU. That saved CPU time goes back into simulation: more units, or the same units with more headroom.

性能之外还有个好处:渲染层放在引擎的可视化系统里。材质、发射器、渲染器用户都能打开、能改,不会锁死在插件代码里。

Performance aside, there is adjustability. The render layer lives in the engine's visual effects system, so materials, emitters and renderers are files a user can open and change, not code buried in the plugin.

最典型的例子是帧间插值。逻辑按固定 tick 走,画面按帧率画,两个频率对不上,只能插值。这件事放在 CPU 上,我们在几万单位下量到大约两成的帧率开销;同样几个插值挪到 GPU 上,开销小到可以忽略。算的是同一个东西,代价差一个数量级。

The clearest case is frame interpolation. Logic steps on a fixed tick, the display draws at its own rate, and the two rates do not line up, so something has to blend them. On the CPU that cost us roughly twenty percent of the frame rate at tens of thousands of units. The same few blends on the GPU are close to free. Identical arithmetic, an order of magnitude apart.

模拟帧率可以比画面低得多 The Simulation May Run Far Slower Than the Display

最实用的一个后果:模拟不必每帧都跑。插值和速度预测在 GPU 上按显示帧补齐,逻辑那边只管按自己的节奏推进。把模拟降到每秒十到十五步(每步约 1/15 秒,六十几毫秒),画面照样连贯。

The most useful consequence: the simulation does not have to run every frame. Interpolation and velocity prediction fill in per display frame on the GPU, so logic can advance at its own pace. Drop the simulation to ten or fifteen steps a second (about one fifteenth of a second each, sixty-odd milliseconds) and the picture still looks continuous.

省下的 CPU 时间很实在:模拟频率降一半,每秒要算的步数就少一半,能装下的单位数量成倍往上走。几千个单位和十万个单位之间差的就是这个,每秒少算几帧,而人眼看不出来。

The CPU time saved is real: halve the simulation rate and you halve the steps per second, which lets the unit count multiply. That is what separates a few thousand units from a hundred thousand: fewer simulation passes per second, at a rate the eye cannot tell apart.

实际场景大概是这样:十万个单位在打,几千发子弹在飞,几千个并发特效同时在炸,外加音效,帧率还在六十以上。这些活没有哪一项能单靠 GPU 兜住。撑住场面的是两边预算分开算:逻辑少算一点,表现多补一点。

What that buys in practice: a hundred thousand units fighting, thousands of projectiles in flight, thousands of concurrent effects on screen, sound on top, and the frame rate still above sixty. No single one of those is carried by the GPU alone. The split budget carries the scene: "compute logic less, fill in appearance more".

代价也在。步长是个旋钮,调长了,两件事会变差。响应延迟:一条指令最晚要等一个步长才落地。预测误差:外推看不见方向突变,急转弯那一两帧会被猜错。所以这个值由玩法定,战术回放和慢节奏对战可以压得很低,讲究跟手的手感就得调回来。

The trade is real. The step length is a knob, and turning it up costs two things. Latency: a command lands up to one step late. Prediction error: extrapolation cannot see a sudden change of direction, so the frame or two around a sharp turn is guessed wrong. Pick the value by gameplay. Replays and slower battles can go very low; anything that needs to feel immediate has to climb back up.

一个系统装下所有画法 One System Holds Every Way of Drawing

这套设计里我最喜欢的一点:同一个单位,各档渲染带可以各用各的画法。同一条渲染管线里,一档用骨骼计算(BoneCS),下一档用顶点动画(VAT),再远一档换成图集公告板(Sprite)。用起来就是在同一个系统里逐档挑一个模式。

One property of this design I like: each render band of a unit can draw its own way. In the same pipeline, one band runs bone-compute (BoneCS), the next vertex animation (VAT), and the farthest one an atlas billboard (Sprite). To the user it is one system with a mode per band.

多部件、多版本走的是同一个思路:一个单位的几个部件、每个部件的几个版本,全收进同一个系统。一个部件一个系统、一个版本一份资产的路子省掉了,开销更少,会不同步的东西也更少。

Parts and versions follow the same idea. A unit's several parts, and each part's several versions, all live in one system instead of one system per part and one asset per version. One system means lower overhead and fewer things that can drift out of step.

为什么是一批一个系统,而不是一个实例一个组件 One System per Batch, Not One Component per Instance

特效上有个很直观的对照实验。场上摆一百颗子弹,每颗挂一个独立的 Niagara 组件,里面只有两个发射器:一个画弹头(core),一个画拖尾(trail)。这点计算量可以忽略,帧率却已经撑不住了。

A blunt experiment shows this on effects. Put a hundred bullets in the level, each carrying its own Niagara component with two emitters: one for the core, one for the trail. The arithmetic is negligible. The frame rate collapses anyway.

开销全在管理这一头。一个 Niagara 组件是一个 UObject:要注册,有生命周期,参与 tick,还要被垃圾回收扫描。一百个这样的对象在内存里散得到处都是,CPU 缓存最怕这个。贵的部分是"管一百个对象",跟"画一百颗子弹"没关系。

The cost sits in administration. A Niagara component is a UObject: it registers, it has a lifetime, it takes part in ticking, and the garbage collector walks it. A hundred of them are scattered across memory, which the CPU cache hates. What is expensive is managing a hundred objects, not drawing a hundred bullets.

换成合批就换了天地:一个子类型对应一个 Niagara 系统。场上所有实例由 CPU 收集成按字段分列的数组,位置、速度、当前帧、样式,一次写进 Niagara 的数据通道。系统里的发射器从通道读这批数据来驱动粒子。还是弹头一个发射器、拖尾一个发射器,跟前面那两个一模一样,只不过现在服务的是全场的子弹。

Batching changes the picture: one subtype, one Niagara system. The CPU gathers every live instance into column-wise arrays: position, velocity, current frame, style. Those arrays go into a Niagara data channel in a single pass, and emitters read that channel to drive their particles. The core emitter and the trail emitter are the same two as before, only now they serve every bullet on the field instead of one.

同样的场景,同样的画面,开销几乎为零:"一百个对象"变成了"每组一百个浮点数"。Agent 渲染和特效渲染用的也是同一套合批模型:模拟侧收集数据、写成数组、交给 Niagara,两边都是每帧一组数组,谁也不用每帧养一堆对象。

Same scene, same picture, close to zero cost, because "a hundred objects" became "a hundred floats per column". Agent rendering and effect rendering share this batched model for the same reason: the simulation side collects, writes arrays, hands them to Niagara, and both sides work in one set of arrays per frame instead of a pile of objects.

代价也得讲清楚:个体不能再自带特殊逻辑。一个实例想要的差异,颜色、大小、播放哪一段,必须能落到数组里的一个或几个字段上。落不进去的,只好另开一个系统,或者另分一档。合批拿每个实例的自由,换每个实例的成本。

The price is explicit: an instance can no longer carry its own logic. Whatever it needs to vary by, colour, size, which segment plays, has to fit into one or a few fields of those arrays. Anything that does not fit needs its own system or its own band. Batching trades each instance's freedom for each instance's cost.

顺带的好处:烘焙出来的资产少了 The Side Benefit: Far Fewer Baked Assets

画法收进同一个系统,烘焙产物也跟着收拢。不管这个单位有几档 LOD,也不管走骨骼模式还是顶点模式,跑的都是同一份材质。骨骼模式连贴图都只有一套,位置与法线两张,所有 LOD 共用。过去一档一份材质、一份贴图的日子,这一版基本结束了。

With drawing unified in one system, the baked output collapses too. However many LOD bands the unit has, and whether it runs bone mode or vertex mode, it runs on one material. Bone mode needs a single texture set, position and normal, shared by every LOD. The old count of one material and one texture per band is essentially gone in this release.

资产一少,烘焙更快,工程更小;出了问题,要翻的东西也少。

Fewer assets also mean faster bakes, smaller projects, and less to dig through when something breaks.

这条线为什么重要 Why the Line Matters

两边都不越界,各自才能简单:逻辑层不用为渲染让步,渲染层不用猜模拟的状态。大规模群体要跑起来,难在同一条数据被两边各算一遍、各解释一遍,单帧算得快也补不回来。线画对了,剩下的只是调数值。

Neither side crosses over, so each stays simple: the logic layer makes no concessions to rendering, the render layer never guesses at simulation state. Getting a large crowd to run is not mainly a matter of one frame computing faster. The expensive part is the same data being computed and interpreted twice, once on each side. Draw the line right and what remains is tuning.

最后把这条线说得更准一点:它管的是权威。逻辑永远不依赖 GPU 算出来的东西。哪一档在画、什么可见、插值到了哪一帧,都是端侧的派生态,不进快照,不进哈希。位置可以为了性能挪,帧间插值已经挪了;权威不能挪。

One last sharpening: the word here is authority. Logic never depends on anything the GPU computed. Which band draws, what is visible, which interpolated frame is on screen: peer-local derived state, never in the snapshot, never in the hash. A location may move for performance, and interpolation already has; authority may not.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels