Skip to content

GPU tile layers for isometric maps (the "shader" render mode) #1897

Closed

Description

@obiot

Summary

An orthogonal tile layer can draw as one quad on the GPU (renderMode: "shader", WebGL 2 and WebGPU): the fragment shader looks each pixel's tile up in an index texture, so the cost no longer grows with the number of tiles on screen. An isometric layer cannot: it always falls back to drawing tile by tile on the CPU, or to a pre-rendered canvas, which animated tiles rule out.

Proposal: the same GPU path for orientation === "isometric" layers, drawing exactly what the CPU renderer draws.

What happens today

  • TMXLayer#_checkShaderEligibility (level/tiled/TMXLayer.js) refuses every orientation but orthogonal: no gpu renderer supported yet for "isometric" orientation.
  • WebGLRenderer#_getTMXGPURendererFor and WebGPURenderer#drawTileLayer only know the orthogonal renderer (video/webgl/renderers/tmxlayer/orthogonal.js, video/webgpu/renderers/tmxlayer/orthogonal.js).
  • So an isometric layer resolves to:
    • "prerender" when asked for: one big canvas, baked again whenever a tile changes where it cannot be redrawn alone, and not available with animated tiles;
    • or "perTile": TMXIsometricRenderer#drawTileLayer walks every visible cell on the CPU each frame, one quad per tile.
  • Both isometric maps in the examples (the Isometric Dungeon's 64 × 64 map of 128 × 256 tiles, and the Tiled map loader's isometric_grass_and_water) take the per-tile path.

Proposal

1. The shader

For each pixel of the layer's quad, the fragment shader:

  1. finds the cells whose tile image could cover the pixel. A tile is drawn as its whole image (tilewidth × tileheight of its tileset), bottom-aligned on its cell's diamond, so with u = col − row and s = col + row these are a small range of u (as wide as the tile is, in half grid widths) and of s (as tall as the tile is, in half grid heights), of matching parity;
  2. walks them front to back, and keeps the first one whose texel is not transparent.

Front to back is the reverse of the order TMXIsometricRenderer#drawTileLayer draws in: rows of constant col + row from the top of the screen down, each from left to right. So the result is the CPU renderer's, tall tiles overlapping the rows behind them included.

Everything else is the orthogonal path's, and should be shared with it rather than copied:

  • the per-layer index texture (16-bit tile id and flip bits per cell), uploaded again only when setTile / clearTile change it;
  • the animation lookup texture, so animated tiles cost nothing more;
  • flips, tileset margin and spacing, mipmap-safe atlas sampling, the layer's opacity and tint.

2. What can take the path

An isometric layer is eligible when:

  • the renderer has GPU tile layers (WebGL 2, WebGPU) and gpuTilemap is on, as for orthogonal;
  • its tiles come from one tileset. The GPU path draws one pass per tileset, which is exact where tiles do not overlap. Isometric tiles do, so tiles of two tilesets in one layer could not be ordered against each other. Several layers, each on its own tileset, are fine. A tile of another tileset set at run time moves the layer back to the CPU path, with a console note;
  • that tileset is an image (not a collection of images), with a tile render size of "Tile size";
  • its tiles are at most 8 grid heights tall and 4 grid widths wide (a fixed bound on the shader's loop). The dungeon's 128 × 256 tiles on a 128 × 64 grid are 4 tall.

A tileset tileoffset can be supported here (unlike the orthogonal path today): with one tileset per layer it shifts the whole layer.

A layer that is not eligible falls back as today, with the reason in the console.

3. Both GPU renderers

  • WebGL 2: an IsometricTMXLayerGPURenderer beside the orthogonal one, returned by _getTMXGPURendererFor("isometric").
  • WebGPU: the WGSL twin, with its uniform block and pipeline registered like the orthogonal one.

Not in scope

  • Staggered, hexagonal and oblique maps. Staggered and hexagonal could follow with the same structure and their own cell maths.
  • Tiles of several tilesets in one isometric layer, and collection-of-image tilesets, on the GPU path.
  • Blending a tile's semi-transparent edge pixels with the tile behind it in the same layer. As on the orthogonal path, the first non-transparent texel is the pixel.
  • Sorting sprites between the tiles of one layer. A layer still draws as one renderable, as it does today.

Done when

  • Same picture as the CPU renderer. Tests draw the same layer through "perTile" and through "shader" and compare the pixels, on WebGL 2 and on WebGPU, for:
    • flat tiles and tall tiles overlapping the rows behind them, and wide tiles;
    • maps that are not square, a view partly and wholly outside the map, and a scrolled or zoomed camera;
    • flipped tiles, animated tiles, tilesets with a margin and spacing, a tileoffset;
    • layer opacity and tint;
    • setTile and clearTile at run time;
    • a layer of an infinite map that starts left of or above the map's origin;
    • the two isometric example maps, at several camera positions.
  • Fallbacks: a layer using two tilesets, a collection of images, or tiles past the size bound draws through the CPU path with its reason logged, and a second tileset set at run time moves a layer back.
  • Examples: the Isometric Dungeon and the Tiled map loader's isometric map draw through the shader path with no change to their code.
  • A benchmark of a large isometric map shows the per-frame cost before and after, for the changelog.
  • Skill and changelog: the melonjs-tilemaps skill's rendering section lists isometric with its conditions and symptom rows, and the engine changelog has a line under Added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions