BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Client render pipeline

One frame of the client, pass by pass, as bf2-rs draws it: what each pass reads and writes, where it lives in crates/bf2-render, which function of the original renderer it reproduces, and what the picture looks like after it. Every image on this page is rendered by our engine from a fixed camera.

BF22142Heroes

The original renderer is RendDX9.dll, a Direct3D 9 module that BF2.exe (and BF2142.exe, BFHeroes.exe) loads at start. Its Renderer::render (RendDX9.dll!0x100300c0; 2142: RendDX9.dll@2142!0x1002cef0) draws a frame in a fixed order of passes and special render queues. bf2-rs keeps that order on wgpu: Renderer::render_ex -> render_ex_passes -> render_frame in crates/bf2-render/src/scene.rs, with the original's material effects (.fx) compiled offline and translated to WGSL (crates/bf2-shaders), so every pass below draws with the same shader the original used. The order and the queue names are documented in notes/subsystems/renderer.md section 2.

Renderer::render Shadow maps Terrain Object queues Sky dome Transparency Post production HUD + overlay beginScene, clear (colour 0, depth 1, stencil 0) LightManager: sun shadow map (casters) XpLights: per-light maps (night) RSMTZOnly, terrain pass 1: Z fill + light buffer step 7: overgrowth, opaque queues, undergrowth step 8: terrain pass 2 (colour), roads step 9: sky dome, sun flare query step 10: RSMTFogPass step 11: transparent queues, decals, particles, water weather / rain, sun flare (1) step 12: endScene (glow queue) step 13: first person (Z cleared, 1P queues) step 14: post effects (NV, TV, glow; remaster: AO, shafts, tonemap) step 15: name tags, flare (2), HUD, console step 16: present
One frame: the sequence of passes in Renderer::render, left the renderer, right what it draws. Steps follow the numbering of renderer.md section 2.

The stages in pictures#

Each pair shows Heroes village (left, 1.126 data: over the roofs from the north-east, the church tower, the houses and the harbour with the sea) and BF2 Strike_at_Karkand (right: the market square at rooftop height) from one fixed camera. A stage switches on its passes in addition to the ones before it (the debug panel's pass switches, bf2_render::debug_view::DebugPass), so each picture shows what the frame looks like once that part has been drawn. A few actors stand in the street so the object, effect and shadow stages have something to show: the Heroes jeep and Panzer IV with a royal soldier hero built from his default items (each item mesh with its crop of the composed soldier atlas, as the client's hero body), a BF2 HMMWV and an idle BF2 soldier (both soldiers posed by the retail animation system), and smoke: on Karkand the retail ambient smoke em_sAmb_OnlySmoke_smoke simulated for 4 s, on village the heavy smoke of a damaged land vehicle (em_dstate_land_heavysmoke_smoke) rising from the Panzer IV, at four times its emission (not retail: village's ambient smoke is too thin to read) for 6 s. Their spot is where the camera sees them whole (village: the plaza behind the gate house, Karkand: the road below the market block); from this high they stay small, so the statics, objects and effects stages add a second row from 14 m off the actors along the same view direction, and the shadow map stage looks at them from 25 m (the sun map covers the casters near the camera). The pictures add the content in the order of the list below; the GPU draws the terrain colour after the opaque objects (the Z fill already holds the terrain's depth), as the diagram shows.

How the pictures are made

python tools/devwiki/render_stages.py runs level_viewer --offscreen --stage all (example crates/bf2-render/examples/level_viewer.rs, the stage code in examples/stages/mod.rs) for both levels and writes docs/dev/engine/img/render/<level>_<nn>_<stage>.png (the close row: <level>_near_<nn>_<stage>.png, --stage-near 14). The actors' spot was found once with --stage-actors auto: the visible street surface rebuilt from the statics' depth buffer, the nearest spot whose footprint is in plain view. Camera, actors, the particle seed and the frame count are fixed, so a re-run writes the same files (--verify checks that). The stage switches are a documentation view, not retail; without --stage nothing changes in the renderer.

1. Z fill: terrain pass 1 writes the depth buffer#

The depth buffer after terrain pass 1, shown as distance from the camera (white near, black far or empty).The depth buffer after terrain pass 1, shown as distance from the camera (white near, black far or empty).
The depth buffer after terrain pass 1, shown as distance from the camera (white near, black far or empty).

Before any colour is drawn, the RSMTZOnly queue (depth-only first-person bundled materials, Renderer::render 0x1003099c) and terrain pass 1 (ZFillLightmap in Terrain::drawHi, 0x100e0fa0) fill the depth buffer, so the expensive colour passes after it are depth-tested against the final terrain. In bf2-rs: render_frame, the terrain light section (Terrain::record_light, crates/bf2-render/src/terrain/mod.rs).

2. Z fill: the terrain light buffer#

The screen-sized terrain light buffer pass 1 writes: light map times the sun shadow, per pixel.The screen-sized terrain light buffer pass 1 writes: light map times the sun shadow, per pixel.
The screen-sized terrain light buffer pass 1 writes: light map times the sun shadow, per pixel.

The same pass writes a screen-sized colour target: the terrain's light map and the sun shadow test per pixel (terrain pass 7, DirectionalLightShadows, when dynamic shadows are on), plus the terrain point lights or, on night maps, the XpLight pass (0x100dfff0). Every later terrain pass and the roads project this buffer instead of lighting again (light_buffer_offset, the half-texel offset 0x10084ea0). Cleared to 0xff000000 (2142: 0).

3. Sun shadow map#

The sun shadow map (light space depth; white is nearest the sun): the casters are the vehicles and the soldier.The sun shadow map (light space depth; white is nearest the sun): the casters are the vehicles and the soldier.
The sun shadow map (light space depth; white is nearest the sun): the casters are the vehicles and the soldier.

Right after beginScene, LightManager::renderShadowMaps (vtable +0x19c) renders the dynamic shadow casters into the sun's depth map with a trapezoidal projection (crates/bf2-render/src/sunshadow.rs, RendDX9.dll!0x101628a0; the map in shadowmap.rs), and at night the per-light maps of the nearest XpLights (xplight.rs, XpLightManager::update 0x100f03e0, renderLightShadowMaps 0x10166fa0). Statics do not cast: their shadows are baked into the light maps. Our renderer keeps one sun map like the original; cascades are a remaster option (crate::enhance::csm, off by default).

4. Terrain colour#

Terrain only: the colour and detail maps lit by the light buffer. Nothing else is drawn yet; the sky is still the clear colour.Terrain only: the colour and detail maps lit by the light buffer. Nothing else is drawn yet; the sky is still the clear colour.
Terrain only: the colour and detail maps lit by the light buffer. Nothing else is drawn yet; the sky is still the clear colour.

Terrain pass 2 (Terrain +0xbc, step 8) draws the colour, detail and low-detail maps per patch with the TerrainShader techniques, reading the light buffer of stage 2 (terrain/mod.rs, record_color; the r.terrain colour section of scene.rs). Patches are culled per frame by the terrain culler (t.cull). The patch grid and each map it draws, decoded from village: what goes into a terrain.

5. Static meshes#

Plus the static meshes: buildings, rocks, fences, props, with their light maps.Plus the static meshes: buildings, rocks, fences, props, with their light maps.
Plus the static meshes: buildings, rocks, fences, props, with their light maps.
The same stage from 14 m off the actors' spot: the street before the objects are drawn.The same stage from 14 m off the actors' spot: the street before the objects are drawn.
The same stage from 14 m off the actors' spot: the street before the objects are drawn.

Step 7's opaque object queues (0x1003bad0): StaticMesh::update (0x100fcc70) picks each visible static's LOD and submits its materials to the shader server's queues, sorted by effect and texture. The visible set comes from the ObjectDrawer's distance and frustum query and the occlusion culler (occlusion.rs, visibility.rs). Code: staticmesh.rs (RaShaderSTM permutations), record_beside in scene.rs.

6. Bundled and skinned meshes#

Plus the bundled meshes (vehicles, weapons) and the skinned meshes (soldiers). Heroes draws them with its cartoon light-warp ramp.Plus the bundled meshes (vehicles, weapons) and the skinned meshes (soldiers). Heroes draws them with its cartoon light-warp ramp.
Plus the bundled meshes (vehicles, weapons) and the skinned meshes (soldiers). Heroes draws them with its cartoon light-warp ramp.
From 14 m: the Panzer IV, the jeep and a royal hero; the HMMWV and the soldier.From 14 m: the Panzer IV, the jeep and a royal hero; the HMMWV and the soldier.
From 14 m: the Panzer IV, the jeep and a royal hero; the HMMWV and the soldier.

Same queues: BundledMesh::submit (0x10105600, bundledmesh.rs, RaShaderBM) for vehicles and weapons with their part matrices, and the skinned meshes (skinned.rs, RaShaderSM, SkinnedMesh::updateLod 0x101a7660) for soldiers, kits and flags, skinned by the animation system's bone matrices (animations). Heroes adds the light-warp ramp and rim light to both and, with renderer.drawOutLines, the RSMTSoldiersAndVehicles / RSMTOutlineEffect queues after the objects (RendDX9.dll@bfh!0x100628d1).

7. Overgrowth and undergrowth#

Plus the vegetation systems: overgrowth trees and bushes, undergrowth grass near the camera.Plus the vegetation systems: overgrowth trees and bushes, undergrowth grass near the camera.
Plus the vegetation systems: overgrowth trees and bushes, undergrowth grass near the camera.

Step 7 wraps the object queues in alpha-to-coverage: first the overgrowth (trees placed by the level's overgrowth layer, overgrowth.rs, DAT_10258f38 +0x34), then the opaque queues, then the undergrowth grass generated per terrain cell around the camera (undergrowth.rs, Undergrowth.fx, 0x1014bc30). Both fade out with distance, so from a camera above the ground they change only the near field.

8. Roads and decals#

Plus the compiled roads (on the terrain, projecting the light buffer) and the decals.Plus the compiled roads (on the terrain, projecting the light buffer) and the decals.
Plus the compiled roads (on the terrain, projecting the light buffer) and the decals.

Renderer::drawRoads (0x1002abb0) draws the RSMTRoad queue after the terrain colour pass: road meshes compiled from the level's editable roads, depth-biased onto the terrain (roads.rs; village's roads on the terrain page). Decals (bullet holes, scorch marks) come later inside the transparency block (decals.rs, DecalManager), switched on here together with the roads.

9. Sky dome#

Plus the sky dome and the sun flare's occlusion query; the level's fog now meets the sky.Plus the sky dome and the sun flare's occlusion query; the level's fog now meets the sky.
Plus the sky dome and the sun flare's occlusion query; the level's fog now meets the sky.

SkyDome::draw (RendDX9.dll!0x101396b0, sky.rs) after all opaque geometry, so it only fills the pixels no object covered; the cloud layers scroll with the global time. The sun flare (SkyDome::drawFlare, 0x10139d10, skyflare.rs) runs three times per frame: the occlusion query here, again after the particles, and its draw after post production.

10. Water#

Plus the water (village: the harbour and the sea beyond it).Plus the water (village: the harbour and the sea beyond it).
Plus the water (village: the harbour and the sea beyond it).

The RSMTWater queue in the transparency block: the terrain's sea tiles (Terrain::drawWater, 0x100e1500, water.rs) with the level's water shader (reflection, depth fade), plus water statics. Heroes 1.126 has its own water path (RendDX9.dll@bfh126!0x101458b9). Whether the camera is under the sea level decides the order of the transparent queues around it.

11. Particles and effects#

Plus the effects: the ambient smoke (sorted sprite particles, lit with the sun and shadow colours).Plus the effects: the ambient smoke (sorted sprite particles, lit with the sun and shadow colours).
Plus the effects: the ambient smoke (sorted sprite particles, lit with the sun and shadow colours).
From 14 m: the smoke column over the Panzer IV (village) and the ambient smoke behind the HMMWV (Karkand).From 14 m: the smoke column over the Panzer IV (village) and the ambient smoke behind the HMMWV (Karkand).
From 14 m: the smoke column over the Panzer IV (village) and the ambient smoke behind the HMMWV (Karkand).

The transparency block (step 11): the transparent queues sorted back to front, decals, the additive queue, the particle systems (ParticleSystemManager +0x2c; sprites particles.rs, mesh particles meshparticles.rs, trails trails.rs, NSA systems nsa.rs), the water, then weather (rain, lightning: weather.rs, 2142 / Heroes snow rain2142.rs). This is the last stage of the retail frame before post production: with the HUD on top it is what the player sees. What a particle effect is made of (the bundle, its systems, the sprite atlases): effects.

12. Post production (and the remaster's image lane)#

Post production with the opt-in remaster image settings: ambient occlusion, light shafts, the float scene with bloom, a filmic tonemap and sharpening (not retail).Post production with the opt-in remaster image settings: ambient occlusion, light shafts, the float scene with bloom, a filmic tonemap and sharpening (not retail).
Post production with the opt-in remaster image settings: ambient occlusion, light shafts, the float scene with bloom, a filmic tonemap and sharpening (not retail).

Retail post production (PostProduction, post.rs: beginScene 0x1004fa00, endScene 0x100504c0) only runs when something asks for it: night vision, the commander's TV, flash bangs, tinnitus, and 2142's glow and fake HDR (post2142.rs; fake HDR 0x1004f550, glow 0x1004fd00). On these daylight levels it changes nothing, so this stage shows the remaster's opt-in image lane instead (crate::enhance, every setting off by default, never on while the D3D9 harness captures), turned up so it reads against stage 11: ssao high (strength 1.5), lightShafts high, hdr with tonemap filmic, bloom 0.8 from 0.6, exposure 0.3, sharpen 0.5 (notes/subsystems/renderer.md "Remaster image pipeline").

13. HUD#

Plus the HUD, in a round the client hosts. Village: the hero's portrait and ability ring, the score (each team's tickets and kills), the round message, the crosshair, health (100), ammo (30) and the weapon bar; the minimap and the chat are closed. Karkand: the minimap with the tickets (110 and 100) and the flags, the game messages, the crosshair, the health bar and the ammo (30 rounds, 5 magazines); no chat line in this frame.Plus the HUD, in a round the client hosts. Village: the hero's portrait and ability ring, the score (each team's tickets and kills), the round message, the crosshair, health (100), ammo (30) and the weapon bar; the minimap and the chat are closed. Karkand: the minimap with the tickets (110 and 100) and the flags, the game messages, the crosshair, the health bar and the ammo (30 rounds, 5 magazines); no chat line in this frame.
Plus the HUD, in a round the client hosts. Village: the hero's portrait and ability ring, the score (each team's tickets and kills), the round message, the crosshair, health (100), ammo (30) and the weapon bar; the minimap and the chat are closed. Karkand: the minimap with the tickets (110 and 100) and the flags, the game messages, the crosshair, the health bar and the ammo (30 rounds, 5 magazines); no chat line in this frame.

The HUD, name tags, the sun flare's draw and the console are drawn last, over the finished image (step 15: HudManager +0x1c). They are the client's, not the level renderer's: the HUD is built from the game's BFMeme .con layout (crates/bf2-hud, the app's client/hud*.rs) and drawn as screen-space quads and text by overlay.rs (0x1008b410); the Flash menus go through Ruffle (Flash menus). So this picture comes from the native client, not the stage viewer: render_stages.py starts it on a round it hosts itself (the listen server in the same process), it spawns at a fixed spawn point (BF2_TEST_SPAWN_PICK, a server test option, not retail) and saves one frame a fixed number of frames later (frame-locked). Village: +loadLevel village +gameMode gpm_tdm and the join to its own server, 150 frames (client script hudshot). Karkand: BF2's command-line hosting, +hostServer 1 +loadLevel strike_at_karkand, which the main menu's first frame starts (MNCommandLine, SwiffHost::startServer BF2.exe!0x00493580) with the server's folder made from the BF2 install, then the spawn screen's team (USMC), kit (assault) and spawn point (client script host-hudshot); the profile has every help message played and the frame comes 1250 frames after the spawn, past the help popup of the kit's weapon, so no help popup covers the HUD. The round runs on the client's own threads, so two captures differ in a few pixels (the idle animation's phase, grass, fireflies, the weapon's sway); the check accepts up to 1 % of the pixels more than 24 levels apart and prints the measured share.

The passes at a glance#

StepPassReadsWritesbf2-rsOriginal
4Sun shadow mapcasters (bundled, skinned)sun depth mapsunshadow.rs, shadowmap.rsLightManager vt+0x19c, RendDX9.dll!0x101628a0
4Per-light maps (night)XpLights, casterslight atlasxplight.rs0x100f03e0, 0x10166fa0
6Z fill + light bufferterrain, light maps, sun mapdepth, light bufferterrain/mod.rs record_lightTerrain::drawHi 0x100e0fa0
7Overgrowthovergrowth layercolour, depthovergrowth.rsDAT_10258f38 +0x34
7Opaque queuesstatics, bundled, skinnedcolour, depthstaticmesh.rs, bundledmesh.rs, skinned.rs, queue.rs0x1003bad0
7Undergrowthterrain cellscolour, depthundergrowth.rs0x1014bc30
8Terrain colourlight buffercolourterrain/mod.rsTerrain +0xbc
8Roadslight buffercolourroads.rs0x1002abb0
9Sky dome, flare querydepthcoloursky.rs, skyflare.rs0x101396b0, 0x10139d10
11Transparency, decals, particles, water, weatherdepthcolourqueue.rs, decals.rs, particles.rs, water.rs, weather.rs0x100e1500, 0x10171ab0
12-14Post productionscene copy, depthcolourpost.rs, post2142.rs0x1004fa00, 0x100504c0, 0x100479f0
13First person1P queuescolour (depth cleared)scene.rs render_frameRenderer::render step 13
15Name tags, flare, HUDcolournametag.rs, skyflare.rs, overlay.rs0x101b0c70, 0x1008b410

Every pass records draw items into a Frame (pipeline.rs); Pipelines::execute turns a frame into one wgpu render pass. The whole frame goes to the GPU queue in one submit (begin_batch / end_batch), and natively the object sections record on worker threads beside the main thread (record_beside, workers.rs) in the same order.

How a frame is budgeted#

The client is CPU-bound almost everywhere: on an RTX 5090 the GPU spends 0.03-0.06 ms per frame in the passes at 1024x768 (BF2_PERF_GPU=1), so the budget is CPU time spent collecting, sorting and recording draws (notes/tracks/performance.md). The counters to watch:

  • F9 debug panel, Perf tab: frame interval, CPU and GPU time (240-frame graph), percentiles, ticks per frame and the per-system CPU times, which include the renderer's scopes (bf2_render::prof). BF2_PERF_OVERLAY=1 (page: ?perf=1) shows the compact frame graph.
  • Renderer scopes (BF2_PERF_SCOPES=1): r.sun shadow, r.light maps, r.terrain light, r.record (r.statics, r.bundled, r.skinned, r.overgrowth, r.undergrowth, r.roads, r.sky and transparency), r.frame submit, r.first person, r.post: one per pass above.
  • Render tab: draws, triangles, passes, pipeline and bind group sets, submits, textures uploaded, pipelines created in the frame (a late pipeline is a hitch), allocations. Drawables tab: what each object drew (LOD, distance, triangles, cull state), with the passes switchable one by one, the same switches these pictures use.
  • Bench: crates/bf2-render/examples/render_bench.rs renders 19 fixed cameras 20 times each and prints the CPU time per render, the scope breakdown and an image hash per camera (two builds that draw the same print the same hashes); the client's scripted runs write perf.txt / frames.csv.

A frame at 60 fps has 16.7 ms for everything (simulation, network, sound, HUD and the render); the level render's share on Karkand is a few ms of CPU, most of it in collecting and recording the object queues.

See also#

  • Engine overview: the frame loop around the render.
  • Shadows and lighting: what is baked, the sun's shadow map, a soldier's light factor. Terrain: the patches the terrain pass draws.
  • notes/subsystems/renderer.md (the original's renderer in detail), notes/subsystems/shaders.md (the effect translation), notes/tracks/performance.md (the measurements).

How it is tested#

  • Frame composition (crates/bf2-render/src/scene.rs): the order of the passes, which culler the lit objects take, what is not drawn from inside a Titan, the indoor cull distances.
  • Single functions against the original's: the terrain's stitching and the particle systems (effects and particles) are compared with the original renderer's own functions, which can be called alone (the oracle).
  • Pictures. The stage pictures on this page are made by the port's renderer from a level (tools/devwiki/render_stages.py), one per pass. They show that each pass draws; they are not a comparison with the original's frame.

A pixel-for-pixel comparison with the original's frame is not claimed anywhere on this page.

In the code#

The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):

CrateWhat it is
crates/bf2-renderthe wgpu renderer
crates/bf2-hudthe in-game HUD: node tree, builder commands, variables, draw lists
crates/bf2-shaderseffect file tooling: shader bytecode parsing and translation to WGSL

Files this page names: crates/bf2-render/src/scene.rs, crates/bf2-render/examples/level_viewer.rs, crates/bf2-render/src/terrain/mod.rs, crates/bf2-render/src/sunshadow.rs, crates/bf2-render/examples/render_bench.rs.

Source: docs/dev/engine/render-pipeline.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.