86 lines
13 KiB
Markdown
86 lines
13 KiB
Markdown
|
|
# ScummVM Rendering Compared With The Windows Rebuild
|
||
|
|
|
||
|
|
## Scope
|
||
|
|
|
||
|
|
This note compares the current ScummVM Ultima 8 engine's Crusader rendering path with `rebuild/remorse`. ScummVM is a modern reimplementation, not an exact transcription of every original runtime behavior. Its source is useful as an implementation and data-format reference, but original-binary parity still requires separate verification. The ScummVM checkout was inspected read-only; no ScummVM code or game data was copied into this repository.
|
||
|
|
|
||
|
|
## Summary
|
||
|
|
|
||
|
|
ScummVM renders the current viewport from live world items. `CurrentMap` maintains a camera-relative fast area over a 64 x 64 chunk grid. `GameMapGump` collects objects from active chunks, and `ItemSorter` rejects completely clipped frame rectangles before constructing overlap dependencies and drawing. Shape data is cached by the archive; raw RLE frames are expanded once into retained 8-bit indexed `ShapeFrame` surfaces. Each paint maps those indexed pixels through the shape palette into ScummVM's managed screen surface.
|
||
|
|
|
||
|
|
The native rebuild instead collects a map scene, prepares and orders render nodes, draws a player-centered backing bitmap, and copies a crop of that bitmap to the window. It currently processes the full map item set for full-scene renders, then uses a background worker to recenter the bitmap and render the player patch. It caches decoded frames, but rebuilds the node/order structures for each full scene render.
|
||
|
|
|
||
|
|
The strongest ScummVM ideas for the rebuild are active-area candidate selection and early frame-rectangle culling. Its fast-area machinery also drives item lifecycle behavior, and its sorter is game-specific; neither should be copied as a rendering-only optimization without preserving those contracts.
|
||
|
|
|
||
|
|
## Pipeline Comparison
|
||
|
|
|
||
|
|
| Concern | ScummVM | Current Windows rebuild |
|
||
|
|
|---|---|---|
|
||
|
|
| World candidates | `CurrentMap` stores map items by chunk. `GameMapGump::PaintThis` visits the 64 x 64 grid and submits items only from chunks marked fast. | `collectSceneItems` stages fixed/live records and expands globs. Full-scene options currently omit `worldRect`, so all renderable map items enter full-scene preparation. |
|
||
|
|
| Camera and active area | `CameraProcess::GetLerped` updates `CurrentMap::updateFastArea` over the camera's from/to positions. Coarse chunk bounds are refined using projected chunk rectangles and a viewport margin. | `paintWindow` crops a 2560 x 1440 player-centered backing image. Near a crop margin it queues a background full-scene recenter. |
|
||
|
|
| Early visibility | `ItemSorter::AddItem` fetches frame dimensions, computes the projected frame rectangle, and rejects a rectangle outside the render clip before overlap/dependency sorting. | `prepareRenderNodes` prepares and orders items before `MapSceneRenderer::render` intersects node rectangles with the output crop. The raster loop avoids off-crop pixels, but off-crop nodes have already been prepared and sorted. |
|
||
|
|
| Occlusion/order | `SortItem::below`, `overlap`, and `occludes` drive dependency links and painter order. ScummVM adds Crusader-specific sprite and inventory-item ordering fields/rules. | A complete sort-item vector is passed to `resolveRenderOrder`; this ports several Pentagram/ScummVM geometry rules but is rebuilt for each full render. |
|
||
|
|
| Sort storage | `GameMapGump` constructs `ItemSorter(2048)`. `BeginDisplayList` recycles the prior list into an unused pool; more nodes are allocated if needed. Per-item dependency storage is also reused. | Render vectors reserve capacity per call, but scene items, nodes, sort items, and the resolved order are reconstructed each full render. |
|
||
|
|
| Shape cache | `ShapeArchive::getShape` lazily caches a `Shape` by shape number. `Shape` builds `ShapeFrame` objects once; each frame retains a decoded CLUT8 pixel surface. | `MapSceneRenderer` retains decoded `DecodedShapeFrame` entries by shape/frame, including palette-expanded pixel colors. Parsed frame collections are local to a render call. |
|
||
|
|
| Pixel path | `RenderSurface::Paint` walks the cached indexed frame surface and maps pixels through the palette into a `Graphics::ManagedSurface`. The engine paints the gump tree, ends painting, and calls `Graphics::Screen::update()`. | The renderer creates a `RenderedMapImage`, converts it to BGR DIB pixels, copies the visible crop, overlays a separate player patch, and calls `StretchDIBits`. |
|
||
|
|
| Update cadence | `Ultima8Engine::paint` paints the desktop/gump tree each frame; `GameMapGump` rebuilds and paints the current display list at the interpolated camera position. | The window paints the cached bitmap on `WM_PAINT`; a worker handles player patches and occasional full-scene recentering. Player patch work is now published before a full refresh. |
|
||
|
|
|
||
|
|
## ScummVM Details
|
||
|
|
|
||
|
|
### Camera-Relative Fast Area
|
||
|
|
|
||
|
|
`CurrentMap` uses a 64 x 64 chunk grid. Its chunk size is 1024 world units for Crusader and 512 for Ultima VIII. `updateFastArea` builds a projected screen-space region from the camera's `from`/`to` positions and map-gump dimensions, expands it by a coarse margin, then checks candidate chunks with `ChunkOnScreen`. Chunks changing state call `setChunkFast` or `unsetChunkFast`, which notify their items through `enterFastArea` and `leaveFastArea`.
|
||
|
|
|
||
|
|
The camera calls this update while interpolating. Therefore the active area follows camera movement before the viewport reaches its new position. Importantly, fast-area transitions are also gameplay lifecycle events: eggs and other items can perform work on entry/exit. A native renderer should initially build a separate render-candidate index unless it is also ready to model those world-state transitions.
|
||
|
|
|
||
|
|
### Clipped Display List And Crusader Ordering
|
||
|
|
|
||
|
|
`GameMapGump::PaintThis` obtains the interpolated camera, determines a roof Z limit, begins a display list with the surface clip rectangle, and submits eligible items from fast chunks. It filters roof/editor content and preserves a special path for the invisible main actor. `ItemSorter::AddItem` computes footpad and frame bounds, then skips a frame that does not intersect the clip before scanning existing visible entries for overlap and ordering dependencies.
|
||
|
|
|
||
|
|
`SortItem::below` includes geometry-based ordering plus special cases for Crusader sprites, inventory items, translucent/animated/flat pieces, roofs, and other shape flags. `PaintDisplayList` recursively paints dependencies and skips occluded nodes. There is an experimental occlusion grouping path behind `SORTITEM_OCCLUSION_EXPERIMENTAL`; it is not the default contract. Like Pentagram, insertion compares a new visible item against prior list entries, so reducing the active/clipped candidate count is central to keeping this sorter practical.
|
||
|
|
|
||
|
|
### Cached Indexed Frames And Managed Surface
|
||
|
|
|
||
|
|
`ShapeArchive::cache` loads a shape on first use and retains it in the archive's shape vector. `Shape` parses each raw frame into an intermediate `RawShapeFrame`, constructs a `ShapeFrame`, then releases the intermediate. `ShapeFrame` expands RLE into a `Graphics::Surface` in CLUT8 format and retains that indexed image. `RenderSurface::Paint` maps the indexed pixels through the shape's native palette into the active 8-, 16-, or 32-bit surface path supported by that method.
|
||
|
|
|
||
|
|
This differs from both nearby implementations: Pentagram's software painter walks RLE runs while drawing, while the rebuild stores palette-expanded decoded pixels in its renderer cache. ScummVM pays a one-time indexed-frame expansion and keeps palette mapping separate from frame pixels.
|
||
|
|
|
||
|
|
`Ultima8Engine::paint` wraps gump rendering with `BeginPainting`/`EndPainting` and then calls `_screen->update()`. The render surface is backed by ScummVM's `Graphics::ManagedSurface`/`Graphics::Screen`; this source path is a CPU pixel renderer feeding ScummVM's presentation system, not a dedicated GPU scene renderer.
|
||
|
|
|
||
|
|
ScummVM also has useful diagnostic hooks: `PAINT_TIMING` can report average paint time, and the debugger command `RenderSurface::benchmark <shape> <frame> <iterations>` measures normal, translucent, mirrored, invisible, and highlighted shape draws. Those hooks can guide a controlled comparison, but no ScummVM benchmark was run for this note.
|
||
|
|
|
||
|
|
## Differences That Matter For The Rebuild
|
||
|
|
|
||
|
|
1. **Candidate selection precedes ordering in ScummVM.** The rebuild's output crop is applied after node preparation and global ordering. A native chunk/spatial index plus conservative frame bounds could reject most map items before decode and sorting.
|
||
|
|
2. **ScummVM keeps rendering in world coordinates.** It doesn't depend on a player-centered cached map image with a recenter edge. The rebuild's current prefetch avoids waiting for that edge, but a full scene refresh still rebuilds the map-wide candidate/node/order pipeline.
|
||
|
|
3. **ScummVM separates frame color indices from screen format.** Its retained CLUT8 frames use less memory than a same-sized RGB cache and can be mapped for the current output format. The tradeoff is palette lookup during each draw. Benchmark memory, decode time, and raster time before changing the rebuild's RGB cache.
|
||
|
|
4. **Its active area is simulation-visible.** `enterFastArea`/`leaveFastArea` have item callbacks. Rendering-only culling should not deactivate gameplay objects or scripts by accident.
|
||
|
|
5. **The sorter is not an interchangeable library.** ScummVM's current comparison paths include Crusader-specific `_sprite`/`_invitem` flags, clip handling, and ordering corrections. Preserve local tests and compare these cases one by one rather than copying the entire class.
|
||
|
|
6. **The display-list pool doesn't eliminate sort cost.** It avoids repeated node allocation, but each paint still gathers active items, tests visible candidates pairwise, resolves dependencies, and rasterizes the display list. Benefits depend on keeping the fast area bounded.
|
||
|
|
|
||
|
|
## Practical Directions
|
||
|
|
|
||
|
|
1. Build a native chunk index over fixed/live map records and glob-expanded placements. Query the viewport plus a configurable overscan margin; keep its membership separate from gameplay activation until item enter/leave behavior is ported.
|
||
|
|
2. Move conservative projected frame-rectangle rejection ahead of node creation, decoded-frame misses, and dependency comparisons. Keep final pixel clipping for correctness.
|
||
|
|
3. Reuse node and dependency storage, or cache static geometry/order and update only mutable entries. Invalidate on position/frame/flag changes, glob hatch changes, roofs, transparency, and world-script mutations.
|
||
|
|
4. Benchmark the three frame representations explicitly: Pentagram's direct RLE walk, ScummVM's cached CLUT8 frame, and the rebuild's palette-expanded decoded frame. Measure a representative map and movement, not only an isolated sprite.
|
||
|
|
5. Record active chunk/item counts, clip rejects, frame-cache hits/misses, sorter comparisons, painted nodes, pixel coverage, and separate collection/sort/raster/present times. The rebuild's instrumented map-1 run previously produced 16,542 candidates, 16,540 decoded nodes, and 1,355 painted nodes in about 0.76 seconds for initial publication; repeated full recenter cost and animation contention need separate measurements.
|
||
|
|
|
||
|
|
The original Crusader executable's 6 x 5 dirty-tile draw system is a separate mechanism documented in [raw-0007-rendering.md](raw-0007-rendering.md). ScummVM's fast chunks control which world items are active and considered; they are not that original screen-tile dirty map.
|
||
|
|
|
||
|
|
## Source References
|
||
|
|
|
||
|
|
### ScummVM
|
||
|
|
|
||
|
|
- [`GameMapGump::PaintThis`](../../../misc/scummvm/engines/ultima/ultima8/gumps/game_map_gump.cpp)
|
||
|
|
- [`ItemSorter::BeginDisplayList`, `AddItem`, and `PaintDisplayList`](../../../misc/scummvm/engines/ultima/ultima8/world/item_sorter.cpp)
|
||
|
|
- [`SortItem` geometry and ordering rules](../../../misc/scummvm/engines/ultima/ultima8/world/sort_item.cpp)
|
||
|
|
- [`CurrentMap::updateFastArea` and chunk transitions](../../../misc/scummvm/engines/ultima/ultima8/world/current_map.cpp), [`CameraProcess`](../../../misc/scummvm/engines/ultima/ultima8/world/camera_process.cpp)
|
||
|
|
- [`ShapeArchive` lazy shape cache](../../../misc/scummvm/engines/ultima/ultima8/gfx/shape_archive.cpp), [`Shape` frame construction](../../../misc/scummvm/engines/ultima/ultima8/gfx/shape.cpp), and [`ShapeFrame` indexed decode](../../../misc/scummvm/engines/ultima/ultima8/gfx/shape_frame.cpp)
|
||
|
|
- [`RenderSurface`](../../../misc/scummvm/engines/ultima/ultima8/gfx/render_surface.cpp), [`Ultima8Engine::paint`](../../../misc/scummvm/engines/ultima/ultima8/ultima8.cpp), and [`RenderSurface::benchmark`](../../../misc/scummvm/engines/ultima/ultima8/misc/debugger.cpp)
|
||
|
|
|
||
|
|
### Native Rebuild
|
||
|
|
|
||
|
|
- [`MapSceneRenderer::render`, item collection, and node preparation](../rebuild/remorse/src/map_renderer.cpp)
|
||
|
|
- [`fullSceneOptions`, `startLiveRenderer`, and `paintWindow`](../rebuild/remorse/src/windowed_game_main.cpp)
|
||
|
|
- [Existing map-renderer format and sorting notes](map-rendering.md)
|