diff --git a/README.md b/README.md index 5734408cc2..07a8f3da58 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ melonJS is designed so you can **focus on making games, not on graphics plumbing - **Complete engine, minimal footprint** — Physics, tilemaps, audio, input, cameras, tweens, particles, UI — a full game stack in a single tree-shakeable ES module. No dependency sprawl, no library stitching. -- **Scenes, loaded in one call** — `level.load(name)` brings an authored scene straight into your world. [Tiled](https://www.mapeditor.org) is a first-class citizen for **2D** — orthogonal, isometric, hexagonal & staggered maps, animated tilesets, collision shapes, object properties, compressed formats, with GPU-accelerated tile rendering on the GPU backends — and **glTF / GLB** is the equivalent for **3D scenes**: author in Blender (or any DCC tool), export a `.glb`, and the whole scene — meshes, materials, cameras, lights, and node animation — loads under a `Camera3d`, no per-mesh wiring. Animated models play back through the same animation API as a 2D `Sprite`. +- **Scenes, loaded in one call** — `level.load(name)` brings an authored scene straight into your world. [Tiled](https://www.mapeditor.org) is a first-class citizen for **2D** — orthogonal, isometric, hexagonal & staggered maps, all four rendered on the GPU (a whole tile layer in a single draw, on WebGPU and WebGL 2), animated tilesets, collision shapes, object properties, compressed formats — and **glTF / GLB** is the equivalent for **3D scenes**: author in Blender (or any DCC tool), export a `.glb`, and the whole scene — meshes, materials, cameras, lights, and node animation — loads under a `Camera3d`, no per-mesh wiring. Animated models play back through the same animation API as a 2D `Sprite`. - **Batteries included, hackable by design** — Get started in minutes with minimal setup. When you need to go deeper: modern class-based architecture, a plugin system for engine extensions, and a clean architecture that's easy to extend without fighting the framework. @@ -117,7 +117,7 @@ UI Scenes - Load a scene in one call with `level.load(name)` — 2D Tiled maps and 3D glTF scenes alike, auto-registered on preload - [Tiled](https://www.mapeditor.org) map format [up to 1.12](https://doc.mapeditor.org/en/stable/reference/tmx-changelog/) built-in support for easy level design - - **GPU-accelerated tile rendering** for orthogonal maps on the GPU backends (WebGL 2 and WebGPU) — each layer draws as a single quad with no per-tile loop, ~5–8× faster than the legacy CPU renderer on dense maps. Honors animated tiles, flip bits, per-layer opacity/tint/blend, and oversized bottom-aligned tiles; falls back transparently to the CPU renderer on isometric/staggered/hexagonal layers or under the Canvas renderer + - **GPU-accelerated tile rendering** for orthogonal, isometric, staggered and hexagonal maps on the GPU backends (WebGL 2 and WebGPU) — each layer draws as a single quad with no per-tile loop, so its cost on the main thread stays flat however many tiles are in view: 85% to 98% less than the CPU renderer on a dense map. Honors animated and flipped tiles, per-layer opacity and tint, tiles larger than the grid and tileset offsets, and `setTile` at run time; falls back transparently to the CPU renderer for a layer it cannot draw (the console says why), on oblique maps and under the Canvas renderer - Uncompressed and [compressed](https://github.com/melonjs/melonJS/tree/master/packages/tiled-inflate-plugin) Plain, Base64, CSV and JSON encoded XML tilemap loading - Orthogonal, Isometric, Hexagonal (both normal and staggered) and Oblique maps - Multiple layers with per-layer alpha, tinting and blend modes (multiple background/foreground, collision and Image layers) diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 510312352e..d2a722b83c 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -4,8 +4,19 @@ ### Added - Transitions: a mask transition can be shaped like an image or a `Sprite`, close on the player with `center`, and fill the shape with `invert`, on `state.transition`, `MaskEffect` and `Trigger` ([#1399](https://github.com/melonjs/melonJS/issues/1399)) +- Tiled: isometric maps are rendered by the GPU on WebGL and WebGPU, as orthogonal ones are, and so are staggered and hexagonal maps. Nothing to enable: a tile layer whose tiles come from one tileset takes it by itself, and any other is drawn as before, the console saying why ([#1897](https://github.com/melonjs/melonJS/issues/1897)) + +### Performance +- Tiled: thanks to the GPU renderer, drawing an isometric tile layer takes about **90%** less of the main thread's time with 1200 tiles in view (0.68 to 0.07 ms a frame) and about **98%** less with 4000 (2.0 to 0.04 ms), and no longer grows with the number of tiles shown. Staggered and hexagonal maps gain the same +- Tiled: for reference, orthogonal tile layers, on the GPU since 19.4, measure about **85%** less with 1000 tiles in view (0.62 to 0.09 ms) and about **97%** less with 3600 (1.9 to 0.06 ms) ### Fixed +- Tiled: a tile layer follows its offset in Tiled, and its own `pos` when the game moves it: its tiles are drawn there, and `getTile()` finds them there. Both were ignored; a map whose tile layers have an offset now looks as it does in Tiled +- Tiled: tiles larger than a cell are drawn wherever they are in view. They were cut off or left out along the edges of the view, above and right of the map, on a map centred in the screen, and once the map itself had scrolled out of view +- Tiled: staggered and hexagonal maps are drawn where Tiled draws them: a tile larger than its cell stands on it, a tileset's `tileoffset` moves its tiles, and maps of odd sizes stay on whole pixels +- Tiled: orthogonal layers drawn by the GPU show the soft edges of large tiles over the tiles behind them, lay large tiles over each other in the map's order, and read a tileset with a `margin` right +- Tiled: an isometric, staggered or hexagonal layer with an `opacity` below 1 fades as one picture on WebGL and WebGPU; each tile showed through the one in front of it +- Tiled: a tile layer removed from its container and added again, and a tile missing from its tileset's image, no longer throw - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it - Text: a stroked multi-line label with `textBaseline: "hanging"` keeps the top of its outline on macOS, where Helvetica's hanging metrics made the ink padding exactly as tall as the ink, shearing its first row off the canvas ([#1870](https://github.com/melonjs/melonJS/issues/1870)) - glTF: a model compressed with Draco or meshopt, which the loader cannot decode, says so in the console, naming the extension, instead of failing on an accessor error that did not mention it diff --git a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md index 51481cf2af..3ea557a668 100644 --- a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md +++ b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md @@ -301,6 +301,17 @@ collision: solid ground comes from collision object layers (below), so a tile set or cleared changes what is drawn and what `cellAt` returns, not what a body runs into. Keep a layer you edit often off `preRender` (see the symptom table). +A tile layer moves with its `pos`. It starts where its offset in Tiled puts +it (Layer → Offset), and the game moves it from there; its tiles are drawn +where it is, and `getTile(x, y)` answers for where they are drawn. Move it +by a difference, not to a point: `pos` is not (0, 0) to begin with on every +map (an infinite one, an oblique one). + +```js +board.pos.y -= 8; // the whole layer, 8 pixels up +board.getTile(pointer.x, pointer.y); // still the tile under the pointer +``` + The map's own custom properties (Map → Map Properties in Tiled) are set on the map object, which `level.getCurrentLevel()` returns once it is loaded: a `theme` string property is `level.getCurrentLevel().theme`. They are set @@ -453,11 +464,32 @@ per-pixel GID lookup in the fragment shader. It requires **all** of: - a renderer that reports GPU tile-layer support (WebGL 2 / WebGPU — not Canvas), - `world.gpuTilemap !== false` (the `gpuTilemap` application setting, default `true`), -- an **orthogonal** layer — isometric, hexagonal, staggered and oblique always - fall back, -- no collection-of-image tileset, no non-zero `tileoffset`, a tileset tile - render size of "Tile size" (`tilerendersize`, not "Map grid size"), and tile - overflow within the shader's 4-cell limit. +- an **orthogonal**, **isometric**, **staggered** or **hexagonal** layer — + oblique always falls back, +- no collection-of-image tileset, and a tileset tile render size of "Tile + size" (`tilerendersize`, not "Map grid size"), +- on an orthogonal map: no non-zero `tileoffset`, and tiles that reach at most + 4 cells past their own; a layer of tiles **larger than a cell** takes them + from one tileset, on a map in the default render order ("right-down"), +- on an isometric, staggered or hexagonal map: every tile of the layer from + **one tileset** (tiles overlap, and their order is kept within a tileset, + not across two: split floor and walls into a layer each, which a map + usually does already), tiles at most 4 cells wide and 8 tall, a grid of + even size (64 × 32, not 65 × 33; a hexagon side of even length), and the + normal blend mode. A `tileoffset` is fine, +- no tile that is not square flipped diagonally (rotated, in Tiled), and no + more columns or rows than the largest texture has pixels (4096 on the + smallest devices). + +A tile set at run time with `setTile` that breaks one of these moves the layer +back to `"perTile"`, with a warning. The two paths draw the same picture, with +one difference: a layer with `opacity` below 1 is faded as one flat picture on +the shader path (as `preRender` does), where tile by tile each tile is faded +over the ones behind it, which shows where tiles overlap. + +The gain is main-thread time: tile by tile, every tile in view is found and +placed in JavaScript each frame (about 2 ms for 4000 tiles); the shader path +draws one quad whatever is in view. When a layer falls back, melonJS `console.warn`s the reason each time the layer is activated (once per page when the renderer has no GPU tile support at @@ -508,7 +540,16 @@ layer into the offscreen-bake path instead. | the level's `name` is not its id after loading | a map custom property named `name` replaced it: map properties are set over the map's own fields | | changing tiles at runtime is slow on a pre-rendered layer | outside an orthogonal map whose tiles fit their cells, a changed tile re-bakes the whole layer before it next draws: keep layers you edit often off `preRender` | | thin lines between tiles on the Canvas renderer, with smoothing on | filtering blends in the next tile of the tileset or its spacing, which a canvas cannot prevent: set `antiAlias: false`, or `preRender` the layer. WebGL and WebGPU keep each tile to itself | +| a tile layer given an offset in Tiled (a roof nudged up, a shadow shifted) is drawn where the others are | before 20.17 a tile layer ignored its offset, and its `pos`; only image layers followed theirs: update | +| after updating to 20.17 a tile layer sits away from where it did | it has an offset in Tiled that was ignored until then, and now moves it as in the editor: clear the layer's Offset in Tiled if it was not meant | +| a map smaller than the screen (centred by `level.load`) had the tops of its tall tiles cut on WebGL or WebGPU | before 20.17 a layer read the camera's view as if it stood at the world's origin, and so did a layer in a container the game had moved: update | +| the tops of tall tiles vanish when the map itself scrolls out of view | before 20.17 a layer was skipped once its grid left the view, though tiles larger than a cell (or moved by a `tileoffset`) still reached in: update | +| an orthogonal layer warns `tiles larger than the grid, of more than one tileset in a layer` | tiles larger than a cell overlap, and the shader path keeps their order within one tileset: put each tileset's large tiles on a layer of their own (trees apart from the ground they stand on) | | tilemap slow on some machines | layer fell back off the shader path — check the console warning for the reason | +| a staggered or hexagonal map sits higher than before 20.17, by how much its tiles are taller than a cell (or by its tileset's `tileoffset`) | it is drawn as Tiled draws it: until then a tile hung from the top of its cell and the offset was ignored, away from the map's objects | +| an isometric (staggered, hexagonal) layer warns `tiles of more than one tileset in an isometric layer` and is slow | the shader path draws such a layer with one tileset: put the tiles of each tileset on a layer of their own, or pack them into one tileset | +| a half-transparent isometric (staggered, hexagonal) layer of tall tiles looks flatter than before 20.17 | on the shader path a layer fades as one picture, and a tile no longer shows through the one in front: set that layer's `renderMode` to `"perTile"` (a `renderMode` custom property in Tiled) for the old look | +| the soft edges of tiles larger than the grid showed a thin dark outline on the shader path | before 20.17 the edge of such a tile was not blended over the tile behind it: update | ## Related skills diff --git a/packages/melonjs/src/application/settings.ts b/packages/melonjs/src/application/settings.ts index 6010e6f77b..425ab6ca7d 100644 --- a/packages/melonjs/src/application/settings.ts +++ b/packages/melonjs/src/application/settings.ts @@ -476,15 +476,18 @@ export type ApplicationSettings = { physic: PhysicsType; /** - * Enable the GPU procedural shader path for orthogonal tile layers - * (backends advertising `renderer.supportsShaderTileLayers` — WebGL 2 - * and WebGPU). When `true` (default), eligible layers render via a - * single quad per tileset + a fragment shader doing per-fragment GID - * lookup, bypassing the per-tile draw loop entirely. Layers that - * don't qualify (Canvas renderer, non-orthogonal, - * collection-of-image tilesets, tilerendersize "grid", non-zero - * tileoffset, oversampled beyond the shader's overflow window) fall - * back to the legacy path automatically. + * Enable the GPU procedural shader path for orthogonal, isometric, + * staggered and hexagonal tile layers (backends advertising + * `renderer.supportsShaderTileLayers` — WebGL 2 and WebGPU). When + * `true` (default), eligible layers render via a single quad per + * tileset + a fragment shader doing per-fragment GID lookup, bypassing + * the per-tile draw loop entirely. Layers that don't qualify fall + * back to the legacy path automatically, saying why in the console: + * among them the Canvas renderer, an oblique map, collection-of-image + * tilesets, tilerendersize "grid", tiles larger than the shader walks; + * on an orthogonal map a non-zero tileoffset; on the others tiles of + * several tilesets in one layer, a grid of odd size, or a blend mode + * other than the normal one. * Set to `false` to disable globally. * @default true */ diff --git a/packages/melonjs/src/camera/camera2d.ts b/packages/melonjs/src/camera/camera2d.ts index 1b8675742c..6dfa51c740 100644 --- a/packages/melonjs/src/camera/camera2d.ts +++ b/packages/melonjs/src/camera/camera2d.ts @@ -1091,8 +1091,14 @@ export default class Camera2d extends Renderable { ); } // non-floating objects use world coordinates, check against - // the camera's visible world area (accounts for position and zoom) - return obj.getBounds().overlaps(this.worldView); + // the camera's visible world area (accounts for position and zoom). + // What a renderable draws can reach past its bounds (a tile layer's + // tiles, taller than their cell, stand above its grid): it says so + // with `getDrawBounds` + const drawn = ( + obj as Renderable & { getDrawBounds?: () => Bounds } + ).getDrawBounds?.(); + return (drawn ?? obj.getBounds()).overlaps(this.worldView); } /** diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index e5bd7d2d72..c1d62d3396 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -5,6 +5,7 @@ import { boundsPool } from "../../physics/bounds.ts"; import Renderable from "../../renderable/renderable.js"; import CanvasRenderer from "../../video/canvas/canvas_renderer"; import { createCanvas } from "../../video/canvas_factory.js"; +import { TMX_ORIENTATIONS } from "../../video/gpu/tmxlayer_orientation.js"; import { TMX_CLEAR_BIT_MASK, TMX_FLIP_AD, @@ -26,6 +27,23 @@ const FLIP_H_BIT = 1 << 0; const FLIP_V_BIT = 1 << 1; const FLIP_AD_BIT = 1 << 2; +// The largest tiles the shader path draws are the bounds of the loops of the +// fragment shaders, which are declared from the same numbers (see +// `TMX_ORIENTATIONS`). An orthogonal tile can reach so many cells past its +// own. +const ORTHOGONAL_MAX_OVERFLOW = TMX_ORIENTATIONS.orthogonal.limits.MAX_OVERFLOW; +// "an isometric", "a staggered", "a hexagonal": a layer's kind, in a reason +const kindOf = (orientation) => { + return `${orientation === "isometric" ? "an" : "a"} ${orientation}`; +}; +const oneTilesetReason = (orientation) => { + return `tiles of more than one tileset in ${kindOf(orientation)} layer`; +}; +// A diagonal flip turns a tile a quarter: one that is not square then covers +// other pixels than its own image's rectangle, which is all the shader looks +// at. +const DIAGONAL_NOT_SQUARE = "a tile that is not square, flipped diagonally"; + // the area an offset camera shows, handed to the tile renderers in its place // (made on first use) let viewRect; @@ -211,8 +229,41 @@ export default class TMXLayer extends Renderable { width: 0, height: 0, }; + /** + * how far a tileset's `tileoffset` moves its tiles, at most, across + * and down: with `maxTileSize`, how far from its cell a tile of this + * layer can be drawn + * @ignore + */ + this.maxTileOffset = { x: 0, y: 0 }; + /** + * The one tileset a layer of an isometric, staggered or hexagonal + * map draws with on the shader path: + * `undefined` for an empty layer (it takes the tileset of the first + * tile set), `null` for a layer that path cannot draw, or that was + * not checked for it. + * @ignore + */ + this._shaderTileset = null; + // how far a `tileoffset` moves tiles, each way + let offsetLeft = 0; + let offsetTop = 0; + let offsetRight = 0; + let offsetBottom = 0; for (let i = 0; i < this.tilesets.length; i++) { const tileset = this.tilesets.getTilesetByIndex(i); + offsetLeft = Math.max(offsetLeft, -tileset.tileoffset.x); + offsetRight = Math.max(offsetRight, tileset.tileoffset.x); + offsetTop = Math.max(offsetTop, -tileset.tileoffset.y); + offsetBottom = Math.max(offsetBottom, tileset.tileoffset.y); + this.maxTileOffset.x = Math.max( + this.maxTileOffset.x, + Math.abs(tileset.tileoffset.x), + ); + this.maxTileOffset.y = Math.max( + this.maxTileOffset.y, + Math.abs(tileset.tileoffset.y), + ); this.maxTileSize.width = Math.max( this.maxTileSize.width, tileset.tilewidth, @@ -222,6 +273,30 @@ export default class TMXLayer extends Renderable { tileset.tileheight, ); } + // A tile larger than its cell reaches past it: up and to the right, + // as a tile stands on the bottom left of its cell (any way on an + // oblique map) + // (the cell is the map renderer's: on a staggered or a hexagonal map + // it can be a pixel less than the map's tile size) + const grid = map.getRenderer?.(); + const cellWidth = grid?.tilewidth ?? this.tilewidth; + const cellHeight = grid?.tileheight ?? this.tileheight; + const overX = Math.max(this.maxTileSize.width - cellWidth, 0); + const overY = Math.max(this.maxTileSize.height - cellHeight, 0); + const upRight = orientation !== "oblique"; + /** + * How far past the layer's grid its tiles can be drawn, on each + * side: by tiles larger than a cell, and by a tileset's + * `tileoffset`. The layer is in view as far as they reach (see + * {@link TMXLayer#getDrawBounds}). + * @ignore + */ + this._reach = { + left: (upRight ? 0 : overX) + offsetLeft, + top: overY + offsetTop, + right: overX + offsetRight, + bottom: (upRight ? 0 : overY) + offsetBottom, + }; /** * All animated tilesets in this layer @@ -359,7 +434,7 @@ export default class TMXLayer extends Renderable { /** * How this layer is rendered. Resolved by `onActivateEvent` to one of: - * - `"shader"` — WebGL2 procedural shader path (single quad per tileset, fragment GID lookup) + * - `"shader"` — GPU procedural shader path, on WebGL 2 and WebGPU, for orthogonal, isometric, staggered and hexagonal maps (single quad per tileset, fragment GID lookup) * - `"prerender"`— offscreen-canvas bake at activation, blitted as one drawImage per frame * - `"perTile"` — per-frame loop, one drawImage per visible tile * @@ -407,14 +482,46 @@ export default class TMXLayer extends Renderable { // layer left of and above the map's origin const bounds = this.getRenderer().getBounds(this); const origin = this._cellOrigin(vector2dPool.get()); - this.pos.x = bounds.left + origin.x; - this.pos.y = bounds.top + origin.y; + /** + * where the layer is in its map, with nothing moving it: what `pos` + * is measured from to know how far the layer was moved + * @ignore + */ + this._mapPos = { x: bounds.left + origin.x, y: bounds.top + origin.y }; this.width = bounds.width; this.height = bounds.height; + // the layer's offset in Tiled moves it from there, as setting `pos` + // does afterwards: its tiles are drawn, and found, where it is + this.pos.x = this._mapPos.x + (+data.offsetx || 0); + this.pos.y = this._mapPos.y + (+data.offsety || 0); vector2dPool.release(origin); boundsPool.release(bounds); } + /** + * The area the layer draws in: its bounds, which are its grid's, and as + * far past them as its tiles reach. A tile larger than its cell, or + * moved by its tileset's `tileoffset`, is drawn outside the grid, and a + * camera has the layer in view while such a tile is (see + * `Camera2d#isVisible`). The layer's bounds, `width` and `height` stay + * the grid's. + * @returns {Bounds} that area, in the same coordinates as `getBounds()` + * @ignore + */ + getDrawBounds() { + const bounds = this.getBounds(); + const reach = this._reach; + /** @ignore */ + this._drawBounds ??= boundsPool.get(); + this._drawBounds.setMinMax( + bounds.left - reach.left, + bounds.top - reach.top, + bounds.right + reach.right, + bounds.bottom + reach.bottom, + ); + return this._drawBounds; + } + /** * where, in world pixels, the layer's renderer draws the tile in the * layer's first column and row, less where it draws the map's tile (0, 0): @@ -439,6 +546,19 @@ export default class TMXLayer extends Renderable { // called when the layer is added to the game world or a container onActivateEvent() { + // afresh: a layer taken out of a container and put back is + // activated again + this.animatedTilesets = []; + // A container with a size of its own (one a level was loaded into) + // is in view by its bounds, before its children are asked: each one + // the layer is in is told that the layer can draw outside them (its + // tiles' reach, its offset, an infinite map's first chunks) + /** @ignore */ + this._toldAncestors = []; + for (let node = this.ancestor; node; node = node.ancestor) { + (node._drawPast ??= new Set()).add(this); + this._toldAncestors.push(node); + } if (this.tilesets) { const tileset = this.tilesets.tilesets; for (let i = 0; i < tileset.length; i++) { @@ -482,10 +602,16 @@ export default class TMXLayer extends Renderable { const renderer = this.getRenderer(); const bounds = renderer.getBounds(this); // an oversized tile reaches past its cell (which way depends on the - // orientation: up on an orthogonal map, down on a hexagonal one), and + // orientation: up and to the right, any way on an oblique map), and // a tileset's offset moves all of its tiles - const overX = Math.max(this.maxTileSize.width - this.tilewidth, 0); - const overY = Math.max(this.maxTileSize.height - this.tileheight, 0); + const overX = Math.max( + this.maxTileSize.width - (renderer.tilewidth ?? this.tilewidth), + 0, + ); + const overY = Math.max( + this.maxTileSize.height - (renderer.tileheight ?? this.tileheight), + 0, + ); let left = bounds.left - overX; let top = bounds.top - overY; let right = bounds.right + overX; @@ -608,6 +734,13 @@ export default class TMXLayer extends Renderable { typeof this.preRender === "boolean" ? this.preRender : root?.preRender; const elig = this._checkShaderEligibility(renderer, gpuAllowed); + /** + * whether the shader path can draw the layer, as its tiles are now + * (kept up to date by `setTile`): a layer asked for that path by + * hand when it cannot is drawn tile by tile + * @ignore + */ + this._shaderFit = elig.ok; const requested = this.renderMode; // explicit "shader" — honor if eligible, warn otherwise @@ -680,7 +813,7 @@ export default class TMXLayer extends Renderable { if (!renderer || renderer.supportsShaderTileLayers !== true) { return { ok: false, reason: "no-gpu-renderer" }; } - if (this.orientation !== "orthogonal") { + if (TMX_ORIENTATIONS[this.orientation] === undefined) { return { ok: false, reason: `no gpu renderer supported yet for "${this.orientation}" orientation`, @@ -689,20 +822,27 @@ export default class TMXLayer extends Renderable { if (!this.tilesets || this.tilesets.tilesets.length === 0) { return { ok: false, reason: "no tilesets" }; } + // the path keeps a texel per cell: no more of them, either way, than + // the largest texture has + const maxTexture = renderer.maxTextureSize ?? Infinity; + if (this.cols > maxTexture || this.rows > maxTexture) { + return { + ok: false, + reason: `more columns or rows than the largest texture has pixels (${maxTexture})`, + }; + } + if (this.orientation !== "orthogonal") { + return this._checkOverlappingShaderEligibility(renderer); + } // the shader iterates a fixed-size loop over candidate cells to // support oversized tiles (tile dim > cell dim) drawn bottom-aligned. // Loop bound is MAX_OVERFLOW + 1; anything beyond would silently clip, // so refuse the shader path for layers with extreme oversampling. - const MAX_OVERFLOW_CELLS = 4; + const MAX_OVERFLOW_CELLS = ORTHOGONAL_MAX_OVERFLOW; for (const ts of this.tilesets.tilesets) { - if (ts.isCollection) { - return { ok: false, reason: "collection-of-image tileset" }; - } - if (ts.tilerendersize !== "tile") { - return { - ok: false, - reason: `tilerendersize "${ts.tilerendersize}" not supported`, - }; + const reason = this._tilesetReason(ts, renderer); + if (reason !== undefined) { + return { ok: false, reason }; } if (ts.tileoffset.x !== 0 || ts.tileoffset.y !== 0) { return { ok: false, reason: "non-zero tileoffset" }; @@ -716,15 +856,314 @@ export default class TMXLayer extends Renderable { }; } } + const overlap = this._checkOrthogonalOverlap(); + if (overlap !== undefined) { + return { ok: false, reason: overlap }; + } + if (this._hasDiagonalFlipNotSquare()) { + return { ok: false, reason: DIAGONAL_NOT_SQUARE }; + } + return { ok: true }; + } + + /** + * Whether a tile of this layer is flipped diagonally and not square, + * which the shader path cannot draw. Only looks at the tiles when a + * tileset has tiles that are not square. + * @returns {boolean} + * @ignore + * @internal + */ + _hasDiagonalFlipNotSquare() { + const tilesets = this.tilesets.tilesets; + if ( + tilesets.every((tileset) => { + return tileset.tilewidth === tileset.tileheight; + }) + ) { + return false; + } + const data = this.layerData; + for (let i = 0; i < data.length; i += 2) { + if (data[i] !== 0 && (data[i + 1] & FLIP_AD_BIT) !== 0) { + const tileset = this.tilesets.getTilesetByGid(data[i]); + if (tileset.tilewidth !== tileset.tileheight) { + return true; + } + } + } + return false; + } + + /** + * Why the shader path cannot draw the tiles of a tileset, on any map, if + * it cannot. + * @param {TMXTileset} tileset - the tileset + * @param {object} [renderer] - the renderer, for its largest texture + * @returns {string|undefined} the reason, or undefined when it can + * @ignore + * @internal + */ + _tilesetReason(tileset, renderer) { + if (tileset.isCollection) { + return "collection-of-image tileset"; + } + if (tileset.tilerendersize !== "tile") { + return `tilerendersize "${tileset.tilerendersize}" not supported`; + } + // an animated tileset has a texel per tile, in one row + const maxTexture = renderer?.maxTextureSize ?? Infinity; + if ( + tileset.isAnimated && + tileset.lastgid - tileset.firstgid + 1 > maxTexture + ) { + return `an animated tileset of more tiles than the largest texture has pixels (${maxTexture})`; + } + return undefined; + } + + /** + * Whether a tileset's tiles are larger than a cell: they overlap the + * cells around theirs. + * @param {TMXTileset} tileset - the tileset + * @returns {boolean} + * @ignore + * @internal + */ + _largerThanCell(tileset) { + return ( + tileset.tilewidth > this.tilewidth || tileset.tileheight > this.tileheight + ); + } + + /** + * Check an orthogonal layer for tiles that overlap in a way the shader + * path does not keep, and note the tilesets its tiles come from. + * + * Tiles the size of a cell never overlap, and any layer of them is fine. + * Tiles larger than a cell lie over their neighbours, the one drawn last + * on top: the shader keeps that order among the tiles of ONE tileset (it + * draws a pass per tileset, each whole pass over the last), and for the + * map's default order alone, from the top down and the left across + * ("right-down"). + * @returns {string|undefined} why the path cannot draw the layer, or + * undefined when it can + * @ignore + * @internal + */ + _checkOrthogonalOverlap() { + /** + * the tilesets an orthogonal layer's tiles come from, when some of + * the map's are larger than a cell (`setTile` adds to them): + * undefined when none is, and nothing can overlap + * @ignore + */ + this._usedTilesets = undefined; + if ( + !this.tilesets.tilesets.some((tileset) => { + return this._largerThanCell(tileset); + }) + ) { + return undefined; + } + const used = new Set(); + const data = this.layerData; + let tileset = null; + for (let i = 0; i < data.length; i += 2) { + const gid = data[i]; + if (gid !== 0 && (tileset === null || !tileset.contains(gid))) { + tileset = this.tilesets.getTilesetByGid(gid); + used.add(tileset); + } + } + this._usedTilesets = used; + return this._orthogonalOverlapReason(); + } + + /** + * Why the tiles of the tilesets an orthogonal layer uses overlap in a way + * the shader path does not keep, if they do (see + * {@link TMXLayer#_checkOrthogonalOverlap}). + * @returns {string|undefined} the reason, or undefined + * @ignore + * @internal + */ + _orthogonalOverlapReason() { + let larger = false; + for (const tileset of this._usedTilesets) { + larger = larger || this._largerThanCell(tileset); + } + if (!larger) { + return undefined; + } + if (this._usedTilesets.size > 1) { + return "tiles larger than the grid, of more than one tileset in a layer"; + } + if (this.renderorder !== "right-down") { + return `render order "${this.renderorder}" with tiles larger than the grid`; + } + return undefined; + } + + /** + * Check whether this layer of an isometric, staggered or hexagonal map + * can take the shader path, and note the tileset it would draw with. + * + * On these maps tiles overlap (each is drawn as its whole image, over + * the ones behind it), and the shader path draws one tileset at a time: + * it can keep the tile drawn last among those of one tileset, not across + * two. So the layer's tiles must all come from the same tileset. Which + * one is found by looking at the tiles, not at the map's list: a map + * commonly has a tileset per layer. + * @returns {{ok: boolean, reason?: string}} + * @ignore + * @internal + */ + _checkOverlappingShaderEligibility(renderer) { + this._shaderTileset = null; + const kind = kindOf(this.orientation); + // Half the tiles of an isometric grid of odd size stand half a pixel + // off. Tile by tile, each is moved to a whole pixel; the shader has + // one quad for all of them, and would draw those a pixel away. (A + // staggered or a hexagonal map is laid out in whole numbers.) + if ( + this.orientation === "isometric" && + (this.tilewidth % 2 !== 0 || this.tileheight % 2 !== 0) + ) { + return { ok: false, reason: `${kind} grid of odd size` }; + } + // Tiles overlap, and the shader draws the layer flat, then blends it + // as one picture: where tile by tile each tile is blended over the + // ones behind it, which shows with every mode but the normal one. + if (this.blendMode !== "normal") { + return { + ok: false, + reason: `blend mode "${this.blendMode}" on ${kind} layer`, + }; + } + const data = this.layerData; + let tileset = null; + for (let i = 0; i < data.length; i += 2) { + const gid = data[i]; + if (gid !== 0 && (tileset === null || !tileset.contains(gid))) { + if (tileset !== null) { + return { ok: false, reason: oneTilesetReason(this.orientation) }; + } + tileset = this.tilesets.getTilesetByGid(gid); + } + } + if (tileset !== null) { + const reason = this._overlappingTilesetReason(tileset, renderer); + if (reason !== undefined) { + return { ok: false, reason }; + } + } + if (this._hasDiagonalFlipNotSquare()) { + return { ok: false, reason: DIAGONAL_NOT_SQUARE }; + } + // an empty layer: its tileset is the first one a tile is set from + this._shaderTileset = tileset ?? undefined; return { ok: true }; } + /** + * Why the shader path of an isometric, staggered or hexagonal map cannot + * draw the tiles of a tileset, if it cannot. + * @param {TMXTileset} tileset - the tileset + * @returns {string|undefined} the reason, or undefined when it can + * @ignore + * @internal + */ + _overlappingTilesetReason(tileset, renderer) { + const unfit = this._tilesetReason(tileset, renderer); + if (unfit !== undefined) { + return unfit; + } + // The shader looks, for each pixel, at every cell whose tile image + // could cover it: a bounded number, of lines of cells and of cells + // in a line. On an isometric map the lines are half a grid height + // apart and their cells a grid width; on the others, a row height + // and two column widths + const { MAX_ROWS, MAX_COLS } = TMX_ORIENTATIONS[this.orientation].limits; + const map = this.getRenderer(); + const isometric = this.orientation === "isometric"; + const maxWidth = + MAX_COLS * (isometric ? this.tilewidth : map.columnwidth * 2); + const maxHeight = + MAX_ROWS * (isometric ? this.tileheight / 2 : map.rowheight); + if (tileset.tilewidth > maxWidth || tileset.tileheight > maxHeight) { + return `tiles larger than the shader's limit (${maxWidth} by ${maxHeight} pixels on this grid)`; + } + return undefined; + } + + /** + * A tile set at run time on a layer on the shader path: one that path + * cannot draw moves the layer back to the legacy renderer. + * + * That is a tile flipped diagonally and not square, and on a layer of an + * isometric, staggered or hexagonal map, which draws with one tileset, a + * tile of another one, or of one the path cannot draw. + * @param {number} gid - the gid of the tile just set + * @param {number} flipMask - its flips + * @ignore + * @internal + */ + _keepShaderEligible(gid, flipMask) { + let reason; + if (this.orientation === "orthogonal") { + // a tileset the layer did not use yet, where tiles can overlap + if (this._usedTilesets !== undefined) { + const tileset = this.tilesets.getTilesetByGid(gid); + if (!this._usedTilesets.has(tileset)) { + this._usedTilesets.add(tileset); + reason = this._orthogonalOverlapReason(); + } + } + } else if (this._shaderTileset?.contains(gid) !== true) { + if (this._shaderTileset) { + reason = oneTilesetReason(this.orientation); + } else { + // an empty layer takes its tileset from its first tile + const tileset = this.tilesets.getTilesetByGid(gid); + reason = this._overlappingTilesetReason( + tileset, + this.parentApp?.renderer, + ); + if (reason === undefined) { + this._shaderTileset = tileset; + } + } + } + if (reason === undefined && (flipMask & FLIP_AD_BIT) !== 0) { + const tileset = this.tilesets.getTilesetByGid(gid); + if (tileset.tilewidth !== tileset.tileheight) { + reason = DIAGONAL_NOT_SQUARE; + } + } + if (reason !== undefined) { + this._shaderFit = false; + this._shaderTileset = null; + // drawn another way for now (by hand): nothing to move back + if (this.renderMode === "shader") { + console.warn( + `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, + ); + this.renderMode = "perTile"; + } + } + } + // called when the layer is removed from the game world or a container onDeactivateEvent() { // renderer-side caches keyed by this layer (e.g. the WebGL2 shader // path's per-layer GID index texture) are cleared from the renderer's // own `reset()` path — tile layers only come and go on game reset. - this.animatedTilesets = undefined; + this.animatedTilesets = []; + for (const node of this._toldAncestors ?? []) { + node._drawPast?.delete(this); + } + this._toldAncestors = undefined; // keep canvasRenderer for reuse — dropping the reference would leak // event listeners registered by CanvasRenderer's constructor } @@ -777,9 +1216,10 @@ export default class TMXLayer extends Renderable { let tile = null; if (this.contains(x, y)) { + // in the map's pixels: back from where the layer was moved to const coord = this.getRenderer().pixelToTileCoords( - x, - y, + x - (this.pos.x - this._mapPos.x), + y - (this.pos.y - this._mapPos.y), vector2dPool.get(), ); tile = this.cellAt(coord.x, coord.y); @@ -825,6 +1265,11 @@ export default class TMXLayer extends Renderable { this.dataVersion++; this.isDirty = true; this._preRenderCell(x, y); + // whichever way the layer is drawn now: it may be asked for the + // shader path again + if (this._shaderFit === true) { + this._keepShaderEligible(cleanGid, this.layerData[idx + 1]); + } return tile; } @@ -969,6 +1414,10 @@ export default class TMXLayer extends Renderable { this.canvasRenderer.renderTarget.invalidate(renderer); renderer.cache.tinted?.delete(this.canvasRenderer.getCanvas()); } + // whether the view is the default camera's, which draws the world + // where its `pos` puts it (any other draws it unmoved): asked of the + // camera, before a plain rectangle stands in for it below + const unmovedWorld = rect.isDefault === false; // the camera's offset (a look ahead, a shake) shifts what it shows // without moving it: the tiles to draw are the ones in that view const offset = rect.offset; @@ -977,13 +1426,48 @@ export default class TMXLayer extends Renderable { viewRect.pos.set(rect.pos.x + offset.x, rect.pos.y + offset.y); rect = viewRect.setSize(rect.width, rect.height); } - // an infinite map's layer that starts left of or above the map's - // origin: its renderer draws the layer's grid from its first cell, - // moved to where that cell's tile is in the world - if (this.startx !== 0 || this.starty !== 0) { + // The view is the camera's, in the world's coordinates, and the layer + // is drawn where its ancestors put it: a world moved to centre a map + // smaller than the view, a container the game moved. Read in the + // layer's own coordinates, the view starts that much earlier. (A + // camera other than the default one draws the world unmoved.) + let shiftX = 0; + let shiftY = 0; + const parent = this.ancestor; + if (parent !== undefined && parent !== null) { + const at = parent.getAbsolutePosition(); + shiftX = at.x; + shiftY = at.y; + if (unmovedWorld) { + let root = parent; + while (root.ancestor) { + root = root.ancestor; + } + shiftX -= root.pos.x; + shiftY -= root.pos.y; + } + } + // A layer moved from its place in the map, by its offset in Tiled or + // by its `pos`, and an infinite map's layer that starts left of or + // above the map's origin: the layer's renderer draws its grid from + // its first cell, moved to where that cell's tile is in the world + const movedX = this.pos.x - this._mapPos.x; + const movedY = this.pos.y - this._mapPos.y; + if ( + movedX !== 0 || + movedY !== 0 || + shiftX !== 0 || + shiftY !== 0 || + this.startx !== 0 || + this.starty !== 0 + ) { const origin = this._cellOrigin(vector2dPool.get()); + origin.set(origin.x + movedX, origin.y + movedY); cellRect ??= new Rect(0, 0, 0, 0); - cellRect.pos.set(rect.pos.x - origin.x, rect.pos.y - origin.y); + cellRect.pos.set( + rect.pos.x - shiftX - origin.x, + rect.pos.y - shiftY - origin.y, + ); cellRect.setSize(rect.width, rect.height); renderer.translate(origin.x, origin.y); renderer.drawTileLayer(this, cellRect); diff --git a/packages/melonjs/src/level/tiled/TMXTileset.js b/packages/melonjs/src/level/tiled/TMXTileset.js index 23f32c8cf3..408ae04e76 100644 --- a/packages/melonjs/src/level/tiled/TMXTileset.js +++ b/packages/melonjs/src/level/tiled/TMXTileset.js @@ -753,6 +753,15 @@ export default class TMXTileset { dy += this._renderDyCenter; } + // a tile the tileset's image does not hold: nothing to draw (see + // `drawTileRaw`) + const frame = this.isCollection + ? undefined + : this.atlas[this.getViewTileId(tmxTile.tileId)]; + if (!this.isCollection && frame === undefined) { + return; + } + // check if any transformation is required if (tmxTile.flipped) { renderer.save(); @@ -779,7 +788,7 @@ export default class TMXTileset { ); } else { // use the tileset texture atlas - const offset = this.atlas[this.getViewTileId(tmxTile.tileId)].offset; + const offset = frame.offset; renderer.drawImage( this.image, offset.x, @@ -860,6 +869,16 @@ export default class TMXTileset { dy += this._renderDyCenter; } + // a tile the tileset's image does not hold (an image cropped since the + // map was saved, a `tilecount` larger than the image has): nothing + // to draw, as in Tiled + const frame = this.isCollection + ? undefined + : this.atlas[this.getViewTileId(gid)]; + if (!this.isCollection && frame === undefined) { + return; + } + // check if any transformation is required if (flipMask !== 0) { renderer.save(); @@ -892,7 +911,7 @@ export default class TMXTileset { dh, ); } else { - const offset = this.atlas[this.getViewTileId(gid)].offset; + const offset = frame.offset; renderer.drawImage( this.image, offset.x, diff --git a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js index 4d1c27eb9f..7ca646e3b3 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js @@ -29,7 +29,7 @@ export default class TMXHexagonalRenderer extends TMXRenderer { * @param {TMXTileMap} map - the TMX map */ constructor(map) { - super(map.cols, map.rows, map.tilewidth & ~1, map.tileheight & ~1); + super(map.cols, map.rows, map.tilewidth, map.tileheight); this.hexsidelength = map.hexsidelength || 0; this.staggerX = map.staggeraxis === "x"; @@ -46,12 +46,22 @@ export default class TMXHexagonalRenderer extends TMXRenderer { } } - this.sideoffsetx = (this.tilewidth - this.sidelengthx) / 2; - this.sideoffsety = (this.tileheight - this.sidelengthy) / 2; + // In whole numbers, as Tiled's own (libtiled's hexagonal + // `RenderParams`): what a side leaves of a tile is halved, and when + // that is an odd number of pixels the half pixel is dropped. + // Columns and rows stay on whole pixels, where Tiled has them + this.sideoffsetx = Math.trunc((map.tilewidth - this.sidelengthx) / 2); + this.sideoffsety = Math.trunc((map.tileheight - this.sidelengthy) / 2); this.columnwidth = this.sideoffsetx + this.sidelengthx; this.rowheight = this.sideoffsety + this.sidelengthy; + // and the cell a tile stands on is made of those: the map's tile + // size, less the pixel dropped (63 x 31 tiles of a staggered map + // stand on 62 x 30 cells) + this.tilewidth = this.columnwidth + this.sideoffsetx; + this.tileheight = this.rowheight + this.sideoffsety; + this.centers = [ new Vector2d(), new Vector2d(), @@ -379,12 +389,32 @@ export default class TMXHexagonalRenderer extends TMXRenderer { * @internal */ drawTileLayer(renderer, layer, rect) { + // The cells whose tile can reach the view. A tile stands on the + // bottom left of its cell, as on every map: one larger than the cell + // reaches up and to the right of it, so the cells that matter past + // the view are those to its left and below it, and a tileset's + // `tileoffset` moves its tiles either way. + const overX = Math.max( + (layer.maxTileSize?.width ?? this.tilewidth) - this.tilewidth, + 0, + ); + const overY = Math.max( + (layer.maxTileSize?.height ?? this.tileheight) - this.tileheight, + 0, + ); + const offsetX = layer.maxTileOffset?.x ?? 0; + const offsetY = layer.maxTileOffset?.y ?? 0; + const left = rect.pos.x - overX - offsetX; + const top = rect.pos.y - offsetY; + const right = rect.right + offsetX; + const bottom = rect.bottom + overY + offsetY; + // the tile at the top-left corner of the area, rounded down to its // column and row (a staggered map's renderer gives a point's place // within its tile too) const startTile = this.pixelToTileCoords( - rect.pos.x, - rect.pos.y, + left, + top, vector2dPool.get(), ).floorSelf(); @@ -403,8 +433,8 @@ export default class TMXHexagonalRenderer extends TMXRenderer { * up due to those tiles being visible as well. How we go up one row * depends on whether we're in the left or right half of the tile. */ - const inUpperHalf = rect.pos.y - startPos.y < this.sideoffsety; - const inLeftHalf = rect.pos.x - startPos.x < this.sideoffsetx; + const inUpperHalf = top - startPos.y < this.sideoffsety; + const inLeftHalf = left - startPos.x < this.sideoffsetx; if (inUpperHalf) { startTile.y--; @@ -452,11 +482,11 @@ export default class TMXHexagonalRenderer extends TMXRenderer { let staggeredRow = this.doStaggerX(startTile.x); // main drawing loop - for (; startPos.y < rect.bottom && startTile.y < endY; ) { + for (; startPos.y < bottom && startTile.y < endY; ) { rowTile.setV(startTile); rowPos.setV(startPos); - for (; rowPos.x < rect.right && rowTile.x < endX; rowTile.x += 2) { + for (; rowPos.x < right && rowTile.x < endX; rowTile.x += 2) { if (rowTile.x >= 0 && rowTile.y >= 0 && rowTile.y < layer.rows) { const idx = (rowTile.y * layerCols + rowTile.x) * 2; const gid = data[idx]; @@ -465,16 +495,23 @@ export default class TMXHexagonalRenderer extends TMXRenderer { if (!tilesetCache.contains(gid)) { tilesetCache = tilesets.getTilesetByGid(gid); } + // standing on its cell, and moved by its + // tileset's offset: where `drawTile` draws it tilesetCache.drawTileRaw( renderer, - rowPos.x, - rowPos.y, + tilesetCache.tileoffset.x + rowPos.x, + tilesetCache.tileoffset.y + + rowPos.y + + (this.tileheight - tilesetCache.tileheight), gid, flipMask, ); } } - rowPos.x += this.tilewidth + this.sidelengthx; + // the next cell of the line, two columns on: where + // `tileToPixelCoords` puts it (a side of odd length + // leaves the tile's width a pixel more than that) + rowPos.x += this.columnwidth * 2; } if (staggeredRow) { @@ -505,7 +542,7 @@ export default class TMXHexagonalRenderer extends TMXRenderer { } // main drawing loop - for (; startPos.y < rect.bottom && startTile.y < endY; startTile.y++) { + for (; startPos.y < bottom && startTile.y < endY; startTile.y++) { rowTile.setV(startTile); rowPos.setV(startPos); @@ -513,7 +550,7 @@ export default class TMXHexagonalRenderer extends TMXRenderer { rowPos.x += this.columnwidth; } - for (; rowPos.x < rect.right && rowTile.x < endX; rowTile.x++) { + for (; rowPos.x < right && rowTile.x < endX; rowTile.x++) { if (rowTile.x >= 0 && rowTile.y >= 0 && rowTile.y < layer.rows) { const idx = (rowTile.y * layerCols + rowTile.x) * 2; const gid = data[idx]; @@ -522,16 +559,23 @@ export default class TMXHexagonalRenderer extends TMXRenderer { if (!tilesetCache.contains(gid)) { tilesetCache = tilesets.getTilesetByGid(gid); } + // standing on its cell, and moved by its + // tileset's offset: where `drawTile` draws it tilesetCache.drawTileRaw( renderer, - rowPos.x, - rowPos.y, + tilesetCache.tileoffset.x + rowPos.x, + tilesetCache.tileoffset.y + + rowPos.y + + (this.tileheight - tilesetCache.tileheight), gid, flipMask, ); } } - rowPos.x += this.tilewidth + this.sidelengthx; + // the next cell of the line, two columns on: where + // `tileToPixelCoords` puts it (a side of odd length + // leaves the tile's width a pixel more than that) + rowPos.x += this.columnwidth * 2; } startPos.y += this.rowheight; } diff --git a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js index 3c884b086c..2f9e2d0d64 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js @@ -159,15 +159,30 @@ export default class TMXIsometricRenderer extends TMXRenderer { // cache a couple of useful references let tileset = layer.tileset; - // get top-left and bottom-right tile position + // get top-left and bottom-right tile position: the view, widened by + // the largest tile of ANY of the layer's tilesets, as a tile is drawn + // from its cell up, and by how far a `tileoffset` moves one. By the + // first tileset's size alone, the tall tiles of another one went + // missing along the bottom of the view, where their cell is below it + // and their image still reaches in, and so did tiles moved into view + // by an offset larger than a tile + // (by a cell at least: the corners below are rounded to whole + // cells, half a cell either way, which a tile narrower than that + // does not make up for) + const reachX = + Math.max(layer.maxTileSize.width, this.tilewidth) + + (layer.maxTileOffset?.x ?? 0); + const reachY = + Math.max(layer.maxTileSize.height, this.tileheight) + + (layer.maxTileOffset?.y ?? 0); const rowItr = this.pixelToTileCoords( - rect.pos.x - tileset.tilewidth, - rect.pos.y - tileset.tileheight, + rect.pos.x - reachX, + rect.pos.y - reachY, vector2dPool.get(), ).floorSelf(); const tileEnd = this.pixelToTileCoords( - rect.pos.x + rect.width + tileset.tilewidth, - rect.pos.y + rect.height + tileset.tileheight, + rect.pos.x + rect.width + reachX, + rect.pos.y + rect.height + reachY, vector2dPool.get(), ).ceilSelf(); diff --git a/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js index 5de0ae9ff9..1f5354a43c 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js @@ -87,22 +87,26 @@ export default class TMXOrthogonalRenderer extends TMXRenderer { let incX = 1; let incY = 1; - // get top-left and bottom-right tile position + // get top-left and bottom-right tile position: the cells whose tile + // can reach the view. A tile larger than its cell is drawn from the + // cell's bottom left corner, so it reaches to the RIGHT of its cell + // and ABOVE it: the cells that matter past the view are those to its + // left and BELOW it. (The rows above were the ones added, so a tile + // more than two cells tall went missing along the bottom of the + // view.) A tileset's `tileoffset` moves its tiles either way. + const overX = Math.max(layer.maxTileSize.width - layer.tilewidth, 0); + const overY = Math.max(layer.maxTileSize.height - layer.tileheight, 0); + const offsetX = layer.maxTileOffset?.x ?? 0; + const offsetY = layer.maxTileOffset?.y ?? 0; const start = this.pixelToTileCoords( - Math.max( - rect.pos.x - Math.max(layer.maxTileSize.width - layer.tilewidth, 0), - 0, - ), - Math.max( - rect.pos.y - Math.max(layer.maxTileSize.height - layer.tileheight, 0), - 0, - ), + Math.max(rect.pos.x - overX - offsetX, 0), + Math.max(rect.pos.y - offsetY, 0), vector2dPool.get(), ).floorSelf(); const end = this.pixelToTileCoords( - rect.pos.x + rect.width + this.tilewidth, - rect.pos.y + rect.height + this.tileheight, + rect.pos.x + rect.width + this.tilewidth + offsetX, + rect.pos.y + rect.height + this.tileheight + overY + offsetY, vector2dPool.get(), ).ceilSelf(); diff --git a/packages/melonjs/src/physics/world.js b/packages/melonjs/src/physics/world.js index a16706e07b..dbfa3d58d6 100644 --- a/packages/melonjs/src/physics/world.js +++ b/packages/melonjs/src/physics/world.js @@ -215,17 +215,23 @@ export default class World extends Container { this.preRender = false; /** - * Enable the WebGL2 procedural shader path for orthogonal tile - * layers. When `true` (default), eligible layers render via a - * single quad per tileset + a fragment shader doing per-pixel GID - * lookup — bypassing the per-tile drawImage loop entirely. - * Supported features on the shader path: animated tiles, all - * three flip bits (H/V/AD), per-layer opacity/tint/blend mode, - * and oversized bottom-aligned tiles up to 4 cells of overflow. - * Layers that don't qualify (Canvas/WebGL1, non-orthogonal, - * collection-of-image tilesets, non-zero `tileoffset`, or tile - * overflow beyond the shader's 4-cell limit) fall back to the - * legacy path automatically. Set to `false` to disable globally. + * Enable the GPU procedural shader path (WebGL 2 and WebGPU) for + * orthogonal, isometric, staggered and hexagonal tile layers. When + * `true` (default), + * eligible layers render via a single quad per tileset + a + * fragment shader doing per-pixel GID lookup — bypassing the + * per-tile drawImage loop entirely. + * Supported features on the shader path: animated tiles, flipped + * tiles, per-layer opacity and tint, and tiles larger than the + * grid: up to 4 cells of overflow on an orthogonal map, up to 4 + * cells wide and 8 tall on the others. + * Layers that don't qualify fall back to the legacy path + * automatically, saying why in the console: among them Canvas, an + * oblique map, collection-of-image tilesets, a `tilerendersize` + * of "grid", larger tiles; on an orthogonal map a non-zero + * `tileoffset`; on the others tiles of several tilesets in one + * layer, a grid of odd size, or a blend mode other than the + * normal one. Set to `false` to disable globally. * @type {boolean} * @default true */ diff --git a/packages/melonjs/src/renderable/container.js b/packages/melonjs/src/renderable/container.js index c7673d4712..c313297482 100644 --- a/packages/melonjs/src/renderable/container.js +++ b/packages/melonjs/src/renderable/container.js @@ -1,5 +1,6 @@ import { colorPool } from "../math/color.ts"; import { Matrix3d } from "../math/matrix3d.ts"; +import { boundsPool } from "../physics/bounds.ts"; import Body from "../physics/builtin/body.js"; import { needsShapeResolution, @@ -1130,6 +1131,41 @@ export default class Container extends Renderable { return obj.length > 0 ? obj[0] : null; } + /** + * The area this container draws in, when something in it draws outside + * the container's own bounds and has said so: a tile layer does (see + * `TMXLayer#getDrawBounds`), whose tiles stand above its grid, which an + * offset can move, and which on an infinite map starts left of and above + * the map's origin. A container with a size of its own (one a level was + * loaded into) is in view by this area, as the layer would be (see + * `Camera2d#isVisible`). + * @returns {Bounds|undefined} that area, or undefined when it is the + * container's bounds (or the container has no size of its own) + * @ignore + */ + getDrawBounds() { + const layers = this._drawPast; + if (layers === undefined || layers.size === 0) { + return undefined; + } + const bounds = this.getBounds(); + if (!bounds.isFinite()) { + return undefined; + } + /** @ignore */ + this._drawBounds ??= boundsPool.get(); + this._drawBounds.setMinMax( + bounds.left, + bounds.top, + bounds.right, + bounds.bottom, + ); + for (const layer of layers) { + this._drawBounds.addBounds(layer.getDrawBounds()); + } + return this._drawBounds; + } + /** * return all child in this container * @returns {Renderable[]} an array of renderable object diff --git a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js new file mode 100644 index 0000000000..b3fe304f8a --- /dev/null +++ b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js @@ -0,0 +1,369 @@ +/** + * What the GPU tile layer renderers need to know of a map orientation, the + * same on WebGL and WebGPU: each backend brings the shader text, and reads + * the rest from here ({@link TMX_ORIENTATIONS}). + * + * - `region`: the part of a layer a view shows, for the tiles of one + * tileset. A region is the quad to draw (`x`, `y`, `width`, `height`, in + * the layer's pixels), the shader's `uVisibleStart` and `uVisibleSize` + * (the quad's corner and size, in the units the orientation's fragment + * shader works in) and its `uOverflow`. + * - `tilesetsOf`: the tilesets a layer is drawn with, one pass each. + * - `draws`: whether a layer can be drawn at all (its `renderMode` can be + * set to "shader" by hand, after it was found unfit for it). + * - `limits`: the bounds of the loops of the orientation's fragment shader, + * declared in it as constants of the same names, and what a layer is + * checked against before it takes the GPU path. + * @import { default as TMXLayer } from "../../level/tiled/TMXLayer.js"; + */ + +/** + * @returns {object} an empty region, for a renderer to fill again and again + * @ignore + */ +export function createRegion() { + return { + x: 0, + y: 0, + width: 0, + height: 0, + startX: 0, + startY: 0, + sizeX: 0, + sizeY: 0, + overflowX: 0, + overflowY: 0, + // a staggered or hexagonal map's: its column width and row height, + // and its stagger axis and index as the shader reads them + staggerX: 0, + staggerY: 0, + staggerMode: 0, + }; +} + +/** + * The cells of an orthogonal layer a view shows, for the tiles of one + * tileset: the quad covers them, and the shader works in cells. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @ignore + * @internal + */ +export function orthogonalRegion(layer, rect, tileset, out) { + const tileWidth = layer.tilewidth; + const tileHeight = layer.tileheight; + + // A tile larger than its cell is drawn from the cell's bottom left + // corner: it reaches to the right of its cell and above it, so the tiles + // of the last column and of the first row reach past the grid. The quad + // covers that too, by the layer's LARGEST tile, so that it is the same + // for every tileset of the layer (the shader drops what a pass has no + // tile for). + const largest = layer.maxTileSize ?? { + width: tileset.tilewidth, + height: tileset.tileheight, + }; + const reachX = Math.max(0, Math.ceil(largest.width / tileWidth) - 1); + const reachY = Math.max(0, Math.ceil(largest.height / tileHeight) - 1); + + // compute the visible region in tile-coord space + const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); + const startTileY = Math.max(-reachY, Math.floor(rect.pos.y / tileHeight)); + const endTileX = Math.min( + layer.cols + reachX, + Math.ceil((rect.pos.x + rect.width) / tileWidth), + ); + const endTileY = Math.min( + layer.rows, + Math.ceil((rect.pos.y + rect.height) / tileHeight), + ); + + // visible rect entirely outside the layer — nothing to draw + if (endTileX <= startTileX || endTileY <= startTileY) { + return false; + } + + // quad world coords + out.x = startTileX * tileWidth; + out.y = startTileY * tileHeight; + out.width = (endTileX - startTileX) * tileWidth; + out.height = (endTileY - startTileY) * tileHeight; + + // visible region in tile units (matches the quad's [0..1] UV span — + // shader recovers the per-fragment tile coord as + // `uVisibleStart + uv * uVisibleSize`) + out.startX = startTileX; + out.startY = startTileY; + out.sizeX = endTileX - startTileX; + out.sizeY = endTileY - startTileY; + + // how many cells a tile reaches past its own, across and up + out.overflowX = Math.max(0, Math.ceil(tileset.tilewidth / tileWidth) - 1); + out.overflowY = Math.max(0, Math.ceil(tileset.tileheight / tileHeight) - 1); + return true; +} + +/** + * The view, cut to where a layer's tiles can be, on whole pixels: the quad + * to draw, for an orientation whose shader works in the layer's pixels. + * @param {object} rect - the visible viewport rect (world coords) + * @param {number} left - where the tiles can be, in the layer's pixels + * @param {number} top - where the tiles can be + * @param {number} right - where the tiles can be + * @param {number} bottom - where the tiles can be + * @param {number} originX - where the shader's pixels are measured from + * @param {number} originY - where the shader's pixels are measured from + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows none of it + * @ignore + */ +function viewIn(rect, left, top, right, bottom, originX, originY, out) { + const x0 = Math.floor(Math.max(rect.pos.x, left)); + const y0 = Math.floor(Math.max(rect.pos.y, top)); + const x1 = Math.ceil(Math.min(rect.pos.x + rect.width, right)); + const y1 = Math.ceil(Math.min(rect.pos.y + rect.height, bottom)); + if (x1 <= x0 || y1 <= y0) { + return false; + } + out.x = x0; + out.y = y0; + out.width = x1 - x0; + out.height = y1 - y0; + out.startX = x0 - originX; + out.startY = y0 - originY; + out.sizeX = out.width; + out.sizeY = out.height; + return true; +} + +/** + * The part of an isometric layer a view shows, for the tiles of one tileset: + * the same on every GPU backend, so their renderers share it. + * + * The quad is the view, cut to where the tileset's tiles can be: a tile is + * drawn as its whole image, bottom-aligned on its cell's diamond and moved by + * the tileset's `tileoffset`, so tall tiles reach above the map's top corner + * and wide ones past its right one. + * + * The shader works in the layer's pixels, measured across from the map's top + * corner and with the `tileoffset` taken off: both are in `startX` / + * `startY`, and the shader needs neither. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @ignore + * @internal + */ +export function isometricRegion(layer, rect, tileset, out) { + const halfWidth = layer.tilewidth / 2; + const halfHeight = layer.tileheight / 2; + const originX = layer.getRenderer().originX; + const offsetX = tileset.tileoffset.x; + const offsetY = tileset.tileoffset.y; + const cols = layer.cols; + const rows = layer.rows; + + // where the tiles can be: from the left of the cell (0, rows - 1) to the + // right of the image of (cols - 1, 0), from the top of the image of + // (0, 0) to the bottom of the diamond of (cols - 1, rows - 1) + const left = originX - rows * halfWidth + offsetX; + const right = originX + (cols - 2) * halfWidth + tileset.tilewidth + offsetX; + const top = layer.tileheight - tileset.tileheight + offsetY; + const bottom = (cols + rows) * halfHeight + offsetY; + + if ( + !viewIn(rect, left, top, right, bottom, originX + offsetX, offsetY, out) + ) { + return false; + } + // how many cells' images reach a pixel: as many across as the image is + // wide in half grid widths, as many down as it is tall in half grid + // heights + out.overflowX = Math.ceil(tileset.tilewidth / halfWidth); + out.overflowY = Math.ceil(tileset.tileheight / halfHeight); + return true; +} + +/** + * The part of a staggered or hexagonal layer a view shows, for the tiles of + * one tileset: the two are one geometry (see `staggered-tmxlayer.frag`), + * the second with cells longer by its `hexsidelength`. + * + * As on an isometric map, the quad is the view, cut to where the tileset's + * tiles can be, and the shader works in the layer's pixels with the + * `tileoffset` taken off. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @ignore + * @internal + */ +export function staggeredRegion(layer, rect, tileset, out) { + const map = layer.getRenderer(); + const columnWidth = map.columnwidth; + const rowHeight = map.rowheight; + const offsetX = tileset.tileoffset.x; + const offsetY = tileset.tileoffset.y; + const cols = layer.cols; + const rows = layer.rows; + + // The cells' boxes: lines a row height apart, and in each, cells two + // column widths apart, every other line shifted by one. A row of the map + // is one line, or two when the stagger axis is x + const lines = map.staggerX ? rows * 2 : rows; + // the last box of a line, in column widths from the first (of a shifted + // line, when the axis is y) + const lastBox = map.staggerX ? cols - 1 : cols * 2 - 1; + // where the tiles can be: from the left of the first box to the right of + // the last one's image, from the top of the first line's images to the + // bottom of the last line's boxes + const left = offsetX; + const right = lastBox * columnWidth + tileset.tilewidth + offsetX; + // (a tile stands on the cell the map lays out, which can be a pixel + // less than the map's tile size: see `TMXHexagonalRenderer`) + const top = map.tileheight - tileset.tileheight + offsetY; + const bottom = (lines - 1) * rowHeight + map.tileheight + offsetY; + + // the shader stands a tile on `uCellSize`, the map's tile size: where + // the cell is shorter, its pixels are measured from as much lower + const shorter = layer.tileheight - map.tileheight; + if ( + !viewIn(rect, left, top, right, bottom, offsetX, offsetY - shorter, out) + ) { + return false; + } + // how many cells' images reach a pixel: as many in a line as the image + // is wide in cell spacings, as many lines as it is tall in row heights + out.overflowX = Math.ceil(tileset.tilewidth / (columnWidth * 2)); + out.overflowY = Math.ceil(tileset.tileheight / rowHeight); + out.staggerX = columnWidth; + out.staggerY = rowHeight; + out.staggerMode = (map.staggerX ? 1 : 0) + (map.staggerEven ? 2 : 0); + return true; +} + +/** + * How many tiles a tileset's image has across: counted from the margin, a + * tile plus the spacing apart, the last column going without its outer + * margin, as Tiled and the tileset's own atlas count them. + * @param {object} tileset - the tileset + * @returns {number} the number of columns + * @ignore + * @internal + */ +export function atlasColumns(tileset) { + return Math.max( + 1, + Math.floor( + (tileset.image.width - tileset.margin + tileset.spacing) / + (tileset.tilewidth + tileset.spacing), + ), + ); +} + +/** + * How many tiles a tileset's image has down (see {@link atlasColumns}). + * @param {object} tileset - the tileset + * @returns {number} the number of rows + * @ignore + * @internal + */ +export function atlasRows(tileset) { + return Math.max( + 1, + Math.floor( + (tileset.image.height - tileset.margin + tileset.spacing) / + (tileset.tileheight + tileset.spacing), + ), + ); +} + +/** + * What the orientations whose tiles overlap share: a layer is drawn with + * ONE tileset. A pass keeps the order of the tiles of one tileset, not + * across two (`layer._shaderTileset`, found by the layer when it is checked + * for this path). + */ +const ONE_TILESET = { + /** + * @param {TMXLayer} layer - the layer + * @param {object[]} scratch - an array to fill + * @returns {object[]} that tileset, or none for an empty layer + */ + tilesetsOf(layer, scratch) { + scratch.length = 0; + if (layer._shaderTileset) { + scratch.push(layer._shaderTileset); + } + return scratch; + }, + /** + * not a layer with no tileset to draw with: one that was never + * checked, or whose tiles come from several (`null`; an empty layer + * has `undefined`, and draws nothing) + * @param {TMXLayer} layer - the layer + * @returns {boolean} whether the layer can be drawn + */ + draws(layer) { + return layer._shaderTileset !== null && layer._shaderFit !== false; + }, +}; + +/** + * The map orientations the GPU tile layer renderers draw. + * @ignore + * @internal + */ +export const TMX_ORIENTATIONS = { + orthogonal: { + region: orthogonalRegion, + /** + * every tileset of the map: a pass shows the tiles of its own + * @param {TMXLayer} layer - the layer + * @returns {object[]} the tilesets + */ + tilesetsOf(layer) { + return layer.tilesets.tilesets; + }, + /** + * not a layer found unfit for this path, or made so by a tile set + * since (a layer never checked is drawn) + * @param {TMXLayer} layer - the layer + * @returns {boolean} whether the layer can be drawn + */ + draws(layer) { + return layer._shaderFit !== false; + }, + // how many cells a tile can reach past its own, across and up + limits: { MAX_OVERFLOW: 4 }, + }, + isometric: { + region: isometricRegion, + ...ONE_TILESET, + // the cells whose image can reach a pixel: rows of them, half a + // grid height apart, and in each, cells a grid width apart. So + // tiles up to MAX_ROWS / 2 grid heights tall, MAX_COLS wide + limits: { MAX_ROWS: 16, MAX_COLS: 4 }, + }, + // one geometry for both: lines of cells, a row height apart, and in + // each, cells two column widths apart + staggered: { + region: staggeredRegion, + ...ONE_TILESET, + staggered: true, + limits: { MAX_ROWS: 16, MAX_COLS: 4 }, + }, + hexagonal: { + region: staggeredRegion, + ...ONE_TILESET, + staggered: true, + limits: { MAX_ROWS: 16, MAX_COLS: 4 }, + }, +}; diff --git a/packages/melonjs/src/video/renderer.js b/packages/melonjs/src/video/renderer.js index 28a65333bc..c4a1b8f5a9 100644 --- a/packages/melonjs/src/video/renderer.js +++ b/packages/melonjs/src/video/renderer.js @@ -211,6 +211,16 @@ export default class Renderer { */ this.supportsShaderTileLayers = false; + /** + * The largest texture this renderer can make, in pixels a side: no + * limit on Canvas. A tile layer drawn by the GPU keeps a texel per + * cell, so a layer with more columns or rows than this is drawn + * tile by tile. + * @type {number} + * @ignore + */ + this.maxTextureSize = Infinity; + /** * Whether this backend can render into a half-float color buffer. * Renderability alone; {@link Renderer#supportsHDR} is the one to diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js similarity index 77% rename from packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js rename to packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js index 1d7e8a79f3..c2cb1d7017 100644 --- a/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js +++ b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js @@ -1,7 +1,16 @@ +import { + atlasColumns, + atlasRows, + createRegion, + TMX_ORIENTATIONS, +} from "../../../gpu/tmxlayer_orientation.js"; import { BufferTextureResource } from "../../../texture/resource.js"; import GLShader from "../../glshader.js"; -import orthogonalTMXLayerFragment from "../../shaders/orthogonal-tmxlayer.frag"; -import orthogonalTMXLayerVertex from "../../shaders/orthogonal-tmxlayer.vert"; +import isometricFragment from "../../shaders/isometric-tmxlayer.frag"; +import orthogonalFragment from "../../shaders/orthogonal-tmxlayer.frag"; +import staggeredFragment from "../../shaders/staggered-tmxlayer.frag"; +import tmxLayerVertex from "../../shaders/tmxlayer.vert"; +import tmxLayerCommonFragment from "../../shaders/tmxlayer-common.frag"; /** * additional imports for TypeScript @@ -9,20 +18,34 @@ import orthogonalTMXLayerVertex from "../../shaders/orthogonal-tmxlayer.vert"; * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; */ +// each orientation's part of the fragment shader, appended to the shared one +const FRAGMENTS = { + orthogonal: orthogonalFragment, + isometric: isometricFragment, + // one geometry: a hexagonal map's cells are longer, no more + staggered: staggeredFragment, + hexagonal: staggeredFragment, +}; + // Default `uTint` when the layer has no tint set. Module-scoped so the // fallback path doesn't allocate per frame. const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); /** - * GPU-accelerated renderer for orthogonal TMX tile layers (WebGL2). Draws - * the visible region of a layer as one screen-aligned quad per tileset - * referenced by the layer — the fragment shader samples a per-layer GID - * index texture and the tileset atlas, eliminating the per-tile draw + * GPU-accelerated renderer for TMX tile layers (WebGL2): what every map + * orientation shares. Draws the visible region of a layer as one + * screen-aligned quad per tileset — the fragment shader samples a per-layer + * GID index texture and the tileset atlas, eliminating the per-tile draw * loop. The visible rect, GID range, tile size, opacity, and tint are * pushed as uniforms; the index texture is uploaded once at activation * and re-uploaded only when `layer.dataVersion` changes (mutations from * `setTile`/`clearTile`). * + * There is one of these per map orientation drawn (see `TMX_ORIENTATIONS` + * in `gpu/tmxlayer_orientation.js`). Two things differ from one to the next: + * its part of the fragment shader (where a cell's tile is, and which cells + * can cover a fragment), and the part of a layer a view shows. + * * The per-layer index texture is a `BufferTextureResource` flowing * through the standard `TextureCache` / batcher path — same lane as * every other texture in the engine. Dynamic unit allocation, correct @@ -39,12 +62,26 @@ const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); * @ignore * @internal */ -export default class OrthogonalTMXLayerGPURenderer { +export default class TMXLayerGPURenderer { /** * @param {WebGLRenderer} renderer - the WebGL renderer instance + * @param {string} orientation - the map orientation to draw, one of + * `TMX_ORIENTATIONS` */ - constructor(renderer) { + constructor(renderer, orientation) { this.renderer = renderer; + /** + * what differs with the map's orientation + * @ignore + * @internal + */ + this.orientation = TMX_ORIENTATIONS[orientation]; + // the bounds of the shader's loops, declared ahead of its own part + const limits = Object.entries(this.orientation.limits) + .map(([name, value]) => { + return `const int ${name} = ${value};`; + }) + .join("\n"); this.gl = renderer.gl; // Standalone `GLShader` (not `ShaderEffect`) so we own both the // vertex and the fragment source — they're paired in GLSL ES 3.00, @@ -57,8 +94,8 @@ export default class OrthogonalTMXLayerGPURenderer { // only the per-fragment math gets cleaner. this.shader = new GLShader( renderer.gl, - orthogonalTMXLayerVertex, - orthogonalTMXLayerFragment, + tmxLayerVertex, + `${tmxLayerCommonFragment}\n${limits}\n${FRAGMENTS[orientation]}`, renderer.shaderPrecision, ); // per-layer `BufferTextureResource`. `Map` (not `WeakMap`) so we @@ -83,6 +120,26 @@ export default class OrthogonalTMXLayerGPURenderer { * @internal */ this._v4 = new Float32Array(4); + /** + * the texture unit of the index texture of the layer being drawn + * @ignore + * @internal + */ + this._indexUnit = 0; + /** + * what the orientation's `region` fills: the quad to draw, + * in the layer's pixels, and what the shader needs to find a + * fragment's place in the layer from its place in the quad + * @ignore + * @internal + */ + this._regionScratch = createRegion(); + /** + * what the orientation's `tilesetsOf` fills, when it needs to + * @ignore + * @internal + */ + this._tilesetsScratch = []; } /** @@ -269,48 +326,18 @@ export default class OrthogonalTMXLayerGPURenderer { } /** - * Draw an orthogonal TMX layer through the shader path. - * @param {TMXLayer} layer - * @param {object} rect - the visible viewport rect (world coords) + * Bind the layer's shader and what all its tilesets share: called for + * the first tileset the view shows something of. + * @param {TMXLayer} layer - the layer + * @returns {object} the quad batcher, on this renderer's shader + * @ignore + * @internal */ - draw(layer, rect) { + _begin(layer) { const renderer = this.renderer; - const tileWidth = layer.tilewidth; - const tileHeight = layer.tileheight; const cols = layer.cols; const rows = layer.rows; - // compute the visible region in tile-coord space - const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); - const startTileY = Math.max(0, Math.floor(rect.pos.y / tileHeight)); - const endTileX = Math.min( - cols, - Math.ceil((rect.pos.x + rect.width) / tileWidth), - ); - const endTileY = Math.min( - rows, - Math.ceil((rect.pos.y + rect.height) / tileHeight), - ); - - // visible rect entirely outside the layer — nothing to draw - if (endTileX <= startTileX || endTileY <= startTileY) { - return; - } - - // quad world coords - const worldX = startTileX * tileWidth; - const worldY = startTileY * tileHeight; - const worldW = (endTileX - startTileX) * tileWidth; - const worldH = (endTileY - startTileY) * tileHeight; - - // visible region size in tile units (matches the quad's [0..1] UV - // span — shader recovers the per-fragment tile coord as - // `uVisibleStart + uv * uVisibleSize`) - const visStartX = startTileX; - const visStartY = startTileY; - const visSizeX = endTileX - startTileX; - const visSizeY = endTileY - startTileY; - // switch the quad batcher to our custom shader. This flushes any // queued sprite vertices first so they render under their original // shader, then re-binds for our pass. @@ -320,7 +347,7 @@ export default class OrthogonalTMXLayerGPURenderer { // path: dynamic unit allocation, correct `boundTextures` tracking, // per-resource premultiply / filter applied automatically const resource = this._getResource(layer); - const indexUnit = batcher.uploadTexture( + this._indexUnit = batcher.uploadTexture( resource, cols, rows, @@ -328,11 +355,9 @@ export default class OrthogonalTMXLayerGPURenderer { ); resource._uploadedVersion = layer.dataVersion; - this.shader.setUniform("uTileIndex", indexUnit); + this.shader.setUniform("uTileIndex", this._indexUnit); this._setV2("uMapSize", cols, rows); - this._setV2("uVisibleStart", visStartX, visStartY); - this._setV2("uVisibleSize", visSizeX, visSizeY); - this._setV2("uCellSize", tileWidth, tileHeight); + this._setV2("uCellSize", layer.tilewidth, layer.tileheight); // the layer's opacity, and the fade of everything it is drawn in: // set on the renderer by the layer's preDraw this.shader.setUniform("uOpacity", renderer.getGlobalAlpha()); @@ -340,10 +365,21 @@ export default class OrthogonalTMXLayerGPURenderer { "uTint", layer.tint ? layer.tint.toArray() : DEFAULT_TINT, ); + return batcher; + } + + /** + * Draw a TMX layer through the shader path. + * @param {TMXLayer} layer + * @param {object} rect - the visible viewport rect (world coords) + */ + draw(layer, rect) { + const region = this._regionScratch; + let batcher; // one pass per tileset — the shader's uGidRange uniform + discard // hides cells outside the active tileset's GID range - const tilesets = layer.tilesets.tilesets; + const tilesets = this.orientation.tilesetsOf(layer, this._tilesetsScratch); for (let i = 0; i < tilesets.length; i++) { const tileset = tilesets[i]; // skip collection-of-image tilesets (caller's eligibility check @@ -352,6 +388,12 @@ export default class OrthogonalTMXLayerGPURenderer { if (tileset.isCollection || tileset.image === undefined) { continue; } + // the view shows nothing of this tileset's tiles + if (!this.orientation.region(layer, rect, tileset, region)) { + continue; + } + batcher ??= this._begin(layer); + const indexUnit = this._indexUnit; const tsW = tileset.tilewidth; const tsH = tileset.tileheight; @@ -362,19 +404,13 @@ export default class OrthogonalTMXLayerGPURenderer { // atlas geometry (margin + spacing) — number of tiles per row/col // in the atlas image - const atlasCols = Math.max( - 1, - Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), - ); - const atlasRows = Math.max( - 1, - Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), - ); + const atlasCols = atlasColumns(tileset); + const atlasRowCount = atlasRows(tileset); // per-tileset animation lookup. Only allocated/bound for // tilesets that actually have animated tiles — static tilesets // pay neither a texture-unit nor an upload. - const tileCount = atlasCols * atlasRows; + const tileCount = atlasCols * atlasRowCount; const animEntry = this._getOrUpdateAnimLookup(tileset, tileCount); if (animEntry !== undefined) { const animUnit = batcher.uploadTexture( @@ -397,14 +433,16 @@ export default class OrthogonalTMXLayerGPURenderer { } this._setV2("uTileSize", tsW, tsH); - this._setV2("uTilesetCols", atlasCols, atlasRows); + this._setV2("uTilesetCols", atlasCols, atlasRowCount); this._setV2("uInvTilesetSize", 1 / atlasW, 1 / atlasH); this._setV4("uTilesetMargin", margin, margin, spacing, spacing); - this._setV2( - "uOverflow", - Math.max(0, Math.ceil(tsW / tileWidth) - 1), - Math.max(0, Math.ceil(tsH / tileHeight) - 1), - ); + this._setV2("uVisibleStart", region.startX, region.startY); + this._setV2("uVisibleSize", region.sizeX, region.sizeY); + this._setV2("uOverflow", region.overflowX, region.overflowY); + if (this.orientation.staggered === true) { + this._setV2("uStagger", region.staggerX, region.staggerY); + this.shader.setUniform("uStaggerMode", region.staggerMode); + } this._setV2("uGidRange", tileset.firstgid, tileset.lastgid); // the tileset samples its mipmaps when the batcher binds it a // mip sampler: the "linear-mipmap" filter, on a linear texture @@ -426,17 +464,17 @@ export default class OrthogonalTMXLayerGPURenderer { // on whole pixels unless `subPixel` is set, as a sprite is: the // tiles move with the quad (see Renderer#_pixelSnapBegin) const snapped = batcher.renderer._pixelSnapBegin( - worldX, - worldY, - worldW, - worldH, + region.x, + region.y, + region.width, + region.height, ); batcher.addQuad( tileset.texture, - worldX, - worldY, - worldW, - worldH, + region.x, + region.y, + region.width, + region.height, 0, 0, 1, diff --git a/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag new file mode 100644 index 0000000000..ea42ad41a0 --- /dev/null +++ b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag @@ -0,0 +1,73 @@ +// The isometric part of the TMX layer fragment shader: appended to +// `tmxlayer-common.frag`. +// +// A fragment's position is in the layer's pixels, measured across from the +// map's top corner (the renderer takes the map's `originX` and the +// tileset's `tileoffset` off before it gets here), so the cell maths below +// is that of `TMXIsometricRenderer` with no origin in it. +// +// The tile of cell (col, row) is drawn as its whole image, bottom-aligned on +// the cell's diamond: with u = col - row and s = col + row, its left edge is +// at (u - 1) half grid widths and its bottom edge at (s + 2) half grid +// heights. So the cells whose image covers a fragment are a run of `u`, as +// long as the image is wide in half grid widths, and a run of `s`, as long +// as it is tall in half grid heights: `uOverflow` holds the two lengths. +// Only pairs of the same parity are cells. +// +// The CPU renderer draws rows of constant `s` from the top of the screen +// down, each from left to right. The last tile drawn is the one in front, +// so the scan here goes the other way, s high→low then u high→low, and puts +// each texel behind the ones before it until the fragment is opaque: an +// opaque texel ends it at once, and the soft edge of a tile shows the tiles +// behind it. + +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in `gpu/tmxlayer_orientation.js`): a +// layer of larger tiles is left to the CPU renderer. + +// the left of the cell's diamond, and as far above its bottom as the tile +// is tall +vec2 tileOrigin(int cx, int cy) { + return vec2( + float(cx - cy - 1) * uCellSize.x * 0.5, + float(cx + cy + 2) * uCellSize.y * 0.5 - uTileSize.y + ); +} + +void main(void) { + vec2 worldPx = uVisibleStart + vRegion * uVisibleSize; + Footprint fp = footprint(worldPx); + + vec2 halfCell = uCellSize * 0.5; + // the rightmost and the lowest cell whose image can reach the fragment + int uMax = int(floor(worldPx.x / halfCell.x)) + 1; + int sMax = int(floor((worldPx.y + uTileSize.y) / halfCell.y)) - 2; + int spanU = int(uOverflow.x + 0.5); + int spanS = int(uOverflow.y + 0.5); + + vec4 result = vec4(0.0); + vec4 texel; + bool opaque = false; + // rows past the map's last one, or before its first, hold no cell: most + // of a fragment's run, along the bottom of the map and above its top + int sLast = int(uMapSize.x) + int(uMapSize.y) - 2; + for (int j = max(sMax - sLast, 0); j < MAX_ROWS; j++) { + int s = sMax - j; + if (j >= spanS || s < 0) break; + // the first u of this row's parity, then every other one + int u = uMax - ((uMax + s) & 1); + for (int i = 0; i < MAX_COLS; i++) { + if (uMax - u >= spanU) break; + if (tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, fp, texel)) { + if (under(result, texel)) { + opaque = true; + break; + } + } + u -= 2; + } + if (opaque) break; + } + if (result.a <= 0.0) discard; + fragColor = layerColor(result); +} diff --git a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag index e81ed0f6fb..ef36adad72 100644 --- a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag +++ b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag @@ -1,14 +1,5 @@ -#version 300 es - -// Fragment shader for the orthogonal TMX layer GPU renderer (WebGL2 / -// GLSL ES 3.00). -// -// Per fragment the shader: -// 1. recovers the world-pixel position from the host UV, -// 2. walks candidate cells (geometric cell + cells whose oversized, -// bottom-aligned tiles could reach this fragment), -// 3. fetches GIDs from the per-layer index texture, and -// 4. samples the tileset atlas at the correct sub-region. +// The orthogonal part of the TMX layer fragment shader: appended to +// `tmxlayer-common.frag`. // // Fast path: when the tileset has no oversized tiles (`uOverflow == (0, 0)`) // — the common case for grid-aligned maps — only the geometric cell can @@ -20,202 +11,64 @@ // Slow path: tiles drawing larger than the cell are bottom-aligned // vertically and left-aligned horizontally. Render order is "right-down": // later cells end up on top, so the candidate loop scans dy high→low, -// dx low→high and picks the FIRST match. -// -// Index texture encoding (`RGBA8`, one cell per texel): -// R = GID low byte -// G = GID high byte (combined: R | (G << 8) = 16-bit GID) -// B = flip mask (bit 0 = H, bit 1 = V, bit 2 = AD) -// A = unused -// -// Animation lookup (`RGBA8`, 1 row, `tileCount` texels wide): per local -// tile id, the CURRENT frame's local id, same R/G byte-pair encoding. -// -// Why `sampler2D` + float decode rather than `usampler2D`: the engine's -// multi-texture default shader declares `uSampler0..uSamplerN-1` as -// `sampler2D` — all of them are active for WebGL's draw-time validation. -// A `usampler2D`-backed `RGBA8UI` texture cached at any of those units -// (units 0..15 on a typical 16-unit fragment stage) would mismatch when -// the default shader next draws sprites, killing every quad with -// `GL_INVALID_OPERATION`. Staying on regular RGBA8 keeps the cache -// path coherent — the cost is one `floor(c * 255 + 0.5)` per fetch. -// -// `texelFetch` is still used (vs `texture()`) for byte-exact reads — -// it bypasses interpolation, so the integer byte values come out -// unmolested even on a normalized-float sampler. - -// The engine's `setPrecision` step injects precision declarations for -// float and int after the `#version` line, using whatever precision the -// renderer was configured with (`highPrecisionShader` setting on the -// Application). Individual shader files don't hardcode precision so the -// engine-wide preference applies. +// dx low→high, the tile in front first, and puts each texel behind the +// ones before it until the fragment is opaque. `uOverflow` is how many +// cells a tile reaches past its own, across and up. -in vec2 vRegion; -in vec4 vColor; - -uniform sampler2D uSampler; // tileset atlas (RGBA) -uniform sampler2D uTileIndex; // per-layer GID index (RGBA8) -uniform sampler2D uAnimLookup; // per-tileset animation table (RGBA8) - -uniform vec2 uMapSize; -uniform vec2 uCellSize; -uniform vec2 uTileSize; -uniform vec2 uOverflow; -uniform vec2 uTilesetCols; -uniform vec2 uInvTilesetSize; -uniform vec4 uTilesetMargin; // (marginX, marginY, spacingX, spacingY) -uniform vec2 uGidRange; // (firstgid, lastgid) -uniform vec2 uVisibleStart; -uniform vec2 uVisibleSize; -uniform int uAnimSize; // number of entries in uAnimLookup, 0 if disabled -uniform float uOpacity; -uniform vec4 uTint; -uniform float uMipmaps; // 1 under the "linear-mipmap" filter, else 0 - -out vec4 fragColor; - -const int MAX_OVERFLOW = 4; - -// Try to render the tile at cell (cx, cy) for the current fragment. -// Returns true and writes the sampled color to `outColor` when the cell -// contains a visible, in-range tile whose pixel covers this fragment. -// Identical logic for both fast and slow paths — GLSL inlines this -// trivially so there's no function-call overhead at runtime. -// -// `dWorldX` / `dWorldY` are the world position's screen derivatives, taken -// in `main` where control flow is still uniform: the atlas coordinate jumps -// from one tile to the next, so its own derivatives would pick a tiny mip -// level along every tile edge. A texel of the atlas is a world pixel, so -// the world's derivatives are the atlas's. -bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out vec4 outColor) { - int mapW = int(uMapSize.x); - int mapH = int(uMapSize.y); - if (cx < 0 || cx >= mapW || cy < 0 || cy >= mapH) { - return false; - } +// `MAX_OVERFLOW`, the bound of both loops, is declared ahead of this file +// (see `TMX_ORIENTATIONS` in `gpu/tmxlayer_orientation.js`): a layer whose +// tiles reach further is left to the CPU renderer. - // `texelFetch` skips filtering — the 8-bit channel values come back - // as normalized floats, decoded to byte ints below - vec4 cellF = texelFetch(uTileIndex, ivec2(cx, cy), 0); - uvec4 cell = uvec4(cellF * 255.0 + 0.5); - int firstGid = int(uGidRange.x); - int gid = int(cell.r) | (int(cell.g) << 8); - if (gid < firstGid || gid > int(uGidRange.y)) { - return false; - } - - vec2 tileWorldOrigin = vec2( +// a tile is bottom-aligned on its cell, and left-aligned +vec2 tileOrigin(int cx, int cy) { + return vec2( float(cx) * uCellSize.x, (float(cy) + 1.0) * uCellSize.y - uTileSize.y ); - vec2 inTile = (worldPx - tileWorldOrigin) / uTileSize; - if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { - return false; - } - - // flip mask + axis-swap trick: see TMX shader-path flip spec. - // AD performs a transpose; with AD set, H and V swap their effective - // axes (matches the legacy `buildFlipTransform`). - int flipMask = int(cell.b); - float flipH = float(flipMask & 1); - float flipV = float((flipMask >> 1) & 1); - float flipAD = float((flipMask >> 2) & 1); - inTile = mix(inTile, inTile.yx, flipAD); - float effH = mix(flipH, flipV, flipAD); - float effV = mix(flipV, flipH, flipAD); - inTile.x = mix(inTile.x, 1.0 - inTile.x, effH); - inTile.y = mix(inTile.y, 1.0 - inTile.y, effV); - - int localId = gid - firstGid; - - // animation: if the tileset has animated tiles, swap the local id for - // its current frame's id via the lookup texture (CPU updates the - // lookup in lockstep with `tileset.update(dt)`). - if (uAnimSize > 0) { - vec4 animF = texelFetch(uAnimLookup, ivec2(localId, 0), 0); - uvec4 animTexel = uvec4(animF * 255.0 + 0.5); - localId = int(animTexel.r) | (int(animTexel.g) << 8); - } - - float row = floor(float(localId) / uTilesetCols.x); - float col = float(localId) - row * uTilesetCols.x; - vec2 tileOriginPx = uTilesetMargin.xy - + vec2(col, row) * (uTileSize + uTilesetMargin.zw); - vec2 texelPx = tileOriginPx + inTile * uTileSize; - // keep the sample half a texel of the level it reads inside the tile, - // so filtering never blends in the next tile of the tileset, or the - // spacing around it, along the tile's edge: a seam between two tiles - // (never past the tile's centre). Without mipmaps that level is the - // full-size art; under them, the deepest level read is the next one up - // once the level is past a whole number, by more than a rounding error: - // drawn at its size, the derivatives are 1 give or take a few ulps, and - // level 0 it is. Sampled nearest, the sample stays in the same texel - float lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); - vec2 inset = min(vec2(0.5 * exp2(lod * uMipmaps)), uTileSize * 0.5); - texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + uTileSize - inset); - vec2 texelUV = texelPx * uInvTilesetSize; - - vec4 sampled = textureGrad( - uSampler, - texelUV, - dWorldX * uInvTilesetSize, - dWorldY * uInvTilesetSize - ); - if (sampled.a <= 0.0) { - return false; - } - outColor = sampled; - return true; -} - -// the layer's opacity and tint applied to a texel. The texel is -// premultiplied, as the blending expects: the opacity scales its color as -// well as its alpha. The tint colors it; its alpha is not an opacity, as on -// a sprite -vec4 layerColor(vec4 texel) { - return texel * uOpacity * vec4(uTint.rgb, 1.0); } void main(void) { vec2 tileCoord = uVisibleStart + vRegion * uVisibleSize; vec2 geomCell = floor(tileCoord); vec2 worldPx = tileCoord * uCellSize; - vec2 dWorldX = dFdx(worldPx); - vec2 dWorldY = dFdy(worldPx); + Footprint fp = footprint(worldPx); int gx = int(geomCell.x); int gy = int(geomCell.y); int overflowX = int(uOverflow.x + 0.5); int overflowY = int(uOverflow.y + 0.5); - vec4 result; + vec4 result = vec4(0.0); // Fast path: tiles fit the cell exactly (the common case) — only the // geometric cell can contain this fragment's tile. if (overflowX == 0 && overflowY == 0) { - if (!tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY, result)) { + if (!tryRenderCell(gx, gy, worldPx, fp, result)) { discard; } fragColor = layerColor(result); return; } - // Slow path: oversized tiles. Walk candidates dy high→low, dx low→high - // and pick the FIRST match — render order is "right-down" so later - // cells go on top. - bool found = false; + // Slow path: oversized tiles. Walk candidates dy high→low, dx low→high, + // front to back — render order is "right-down" so later cells go on + // top — until the fragment is opaque. + vec4 texel; + bool opaque = false; for (int idy = 0; idy <= MAX_OVERFLOW; idy++) { int dy = MAX_OVERFLOW - idy; if (dy > overflowY) continue; for (int dx = 0; dx <= MAX_OVERFLOW; dx++) { if (dx > overflowX) break; - if (tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY, result)) { - found = true; - break; + if (tryRenderCell(gx - dx, gy + dy, worldPx, fp, texel)) { + if (under(result, texel)) { + opaque = true; + break; + } } } - if (found) break; + if (opaque) break; } - if (!found) discard; + if (result.a <= 0.0) discard; fragColor = layerColor(result); } diff --git a/packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag new file mode 100644 index 0000000000..d3324e176e --- /dev/null +++ b/packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag @@ -0,0 +1,85 @@ +// The staggered and hexagonal part of the TMX layer fragment shader: +// appended to `tmxlayer-common.frag`. +// +// Both maps are one geometry, in the layer's pixels (the renderer takes the +// tileset's `tileoffset` off before a position gets here). Cells stand in +// lines, a `rowheight` apart down the screen; in a line they are two +// `columnwidth` apart, and every other line is shifted by one `columnwidth` +// (which ones, `staggerindex` says). A hexagonal map's cells are longer by +// its `hexsidelength`, which is in those two lengths already. +// +// What `staggeraxis` changes is which cell a place in a line is: +// "y": a line is a row of the map, and its cells are that row's columns; +// "x": a row of the map is two lines, its columns alternating between +// them, so a line holds every other column of one row. +// +// The tile of a cell is drawn as its whole image, standing on the bottom +// left of the cell. So the cells whose image covers a fragment are a run of +// lines, as long as the image is tall in row heights, and in each a run of +// cells, as long as it is wide in cell spacings: `uOverflow` holds the two +// lengths. The CPU renderer draws the lines from the top of the screen +// down, each from the left: the scan here goes the other way, and puts each +// texel behind the ones before it until the fragment is opaque. +// +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in `gpu/tmxlayer_orientation.js`): a +// layer of larger tiles is left to the CPU renderer. + +uniform vec2 uStagger; // (columnwidth, rowheight) +uniform float uStaggerMode; // 1 when the stagger axis is x, + 2 when the index is even + +// whether a line is one of the shifted ones: 1, or 0 +int shifted(int line) { + return (line & 1) ^ (int(uStaggerMode) >> 1); +} + +// the bottom left of the cell's box, less the tile's height +vec2 tileOrigin(int cx, int cy) { + bool staggerX = (int(uStaggerMode) & 1) == 1; + // the cell's line, and how far along it the cell is + int line = staggerX ? 2 * cy + shifted(cx) : cy; + float x = staggerX + ? float(cx) * uStagger.x + : float(2 * cx + shifted(cy)) * uStagger.x; + return vec2(x, float(line) * uStagger.y + uCellSize.y - uTileSize.y); +} + +void main(void) { + vec2 worldPx = uVisibleStart + vRegion * uVisibleSize; + Footprint fp = footprint(worldPx); + + bool staggerX = (int(uStaggerMode) & 1) == 1; + float spacing = uStagger.x * 2.0; + // the lowest line whose image can reach the fragment + int lineMax = int(floor((worldPx.y - uCellSize.y + uTileSize.y) / uStagger.y)); + int spanK = int(uOverflow.x + 0.5); + int spanL = int(uOverflow.y + 0.5); + // lines past the map's last one, or before its first, hold no cell + int lineLast = (staggerX ? 2 * int(uMapSize.y) : int(uMapSize.y)) - 1; + + vec4 result = vec4(0.0); + vec4 texel; + bool opaque = false; + for (int j = max(lineMax - lineLast, 0); j < MAX_ROWS; j++) { + int line = lineMax - j; + if (j >= spanL || line < 0) break; + int shift = shifted(line); + // the rightmost cell of the line whose image can reach the fragment + int kMax = int(floor((worldPx.x - float(shift) * uStagger.x) / spacing)); + for (int i = 0; i < MAX_COLS; i++) { + if (i >= spanK) break; + int k = kMax - i; + int cx = staggerX ? 2 * k + shift : k; + int cy = staggerX ? line >> 1 : line; + if (tryRenderCell(cx, cy, worldPx, fp, texel)) { + if (under(result, texel)) { + opaque = true; + break; + } + } + } + if (opaque) break; + } + if (result.a <= 0.0) discard; + fragColor = layerColor(result); +} diff --git a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag new file mode 100644 index 0000000000..d6fc5fa175 --- /dev/null +++ b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag @@ -0,0 +1,201 @@ +#version 300 es +// Full precision, whatever the application asked for its other shaders +// (`highPrecisionShader`): a fragment's position is in pixels of the map, +// and a gid has 16 bits, which the 16 bits a phone can give a `mediump` +// value do not hold past a small map. The engine's `setPrecision` step +// leaves a shader that declares its own alone. +precision highp float; +precision highp int; + +// Fragment shader for the TMX layer GPU renderers (WebGL2 / GLSL ES 3.00): +// the part every map orientation shares. An orientation's own file follows +// it in the program (`orthogonal-tmxlayer.frag`, `isometric-tmxlayer.frag`, +// `staggered-tmxlayer.frag` for staggered and hexagonal maps) and gives the two things that differ: where a cell's tile is drawn +// (`tileOrigin`), and which cells could cover a fragment, front to back +// (`main`). +// +// Per fragment, `main`: +// 1. recovers the fragment's position in the layer from the host UV, +// 2. walks the cells whose tile could cover it, the one drawn last first, +// 3. and for each, `tryRenderCell` fetches the GID from the per-layer +// index texture and samples the tileset atlas at the right sub-region. +// Each texel found goes behind the ones before it (`under`), until the +// fragment is opaque: the soft edge of a tile shows the tile behind it, as +// it does when the tiles are drawn one over the other. +// +// Index texture encoding (`RGBA8`, one cell per texel): +// R = GID low byte +// G = GID high byte (combined: R | (G << 8) = 16-bit GID) +// B = flip mask (bit 0 = H, bit 1 = V, bit 2 = AD) +// A = unused +// +// Animation lookup (`RGBA8`, 1 row, `tileCount` texels wide): per local +// tile id, the CURRENT frame's local id, same R/G byte-pair encoding. +// +// Why `sampler2D` + float decode rather than `usampler2D`: the engine's +// multi-texture default shader declares `uSampler0..uSamplerN-1` as +// `sampler2D` — all of them are active for WebGL's draw-time validation. +// A `usampler2D`-backed `RGBA8UI` texture cached at any of those units +// (units 0..15 on a typical 16-unit fragment stage) would mismatch when +// the default shader next draws sprites, killing every quad with +// `GL_INVALID_OPERATION`. Staying on regular RGBA8 keeps the cache +// path coherent — the cost is one `floor(c * 255 + 0.5)` per fetch. +// +// `texelFetch` is still used (vs `texture()`) for byte-exact reads — +// it bypasses interpolation, so the integer byte values come out +// unmolested even on a normalized-float sampler. + +in vec2 vRegion; +in vec4 vColor; + +uniform sampler2D uSampler; // tileset atlas (RGBA) +uniform sampler2D uTileIndex; // per-layer GID index (RGBA8) +uniform sampler2D uAnimLookup; // per-tileset animation table (RGBA8) + +uniform vec2 uMapSize; +uniform vec2 uCellSize; +uniform vec2 uTileSize; +uniform vec2 uOverflow; // how far a tile reaches past its cell (see each `main`) +uniform vec2 uTilesetCols; +uniform vec2 uInvTilesetSize; +uniform vec4 uTilesetMargin; // (marginX, marginY, spacingX, spacingY) +uniform vec2 uGidRange; // (firstgid, lastgid) +uniform vec2 uVisibleStart; +uniform vec2 uVisibleSize; +uniform int uAnimSize; // number of entries in uAnimLookup, 0 if disabled +uniform float uOpacity; +uniform vec4 uTint; +uniform float uMipmaps; // 1 under the "linear-mipmap" filter, else 0 + +out vec4 fragColor; + +// Where the tile of cell (cx, cy) is drawn from: the top left corner of its +// image, in the same pixels as `worldPx`. Defined per orientation. +vec2 tileOrigin(int cx, int cy); + +// What sampling the atlas needs of a fragment, the same for every cell +// tried for it: taken once, in `main`, where control flow is still uniform. +// +// `gradX` / `gradY` are the atlas coordinate's screen derivatives. The atlas +// coordinate itself jumps from one tile to the next, so its own derivatives +// would pick a tiny mip level along every tile edge; a texel of the atlas is +// a world pixel, so the world's derivatives are the atlas's. +// +// `inset` keeps a sample half a texel of the level it reads inside its tile, +// so filtering never blends in the next tile of the tileset, or the spacing +// around it, along the tile's edge: a seam between two tiles (never past the +// tile's centre). Without mipmaps that level is the full-size art; under +// them, the deepest level read is the next one up once the level is past a +// whole number, by more than a rounding error: drawn at its size, the +// derivatives are 1 give or take a few ulps, and level 0 it is. Sampled +// nearest, the sample stays in the same texel +struct Footprint { + vec2 gradX; + vec2 gradY; + vec2 inset; +}; + +Footprint footprint(vec2 worldPx) { + vec2 dWorldX = dFdx(worldPx); + vec2 dWorldY = dFdy(worldPx); + float lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); + vec2 inset = min(vec2(0.5 * exp2(lod * uMipmaps)), uTileSize * 0.5); + return Footprint(dWorldX * uInvTilesetSize, dWorldY * uInvTilesetSize, inset); +} + +// Try to render the tile at cell (cx, cy) for the current fragment. +// Returns true and writes the sampled color to `outColor` when the cell +// contains a visible, in-range tile whose pixel covers this fragment. +// Identical logic for both fast and slow paths — GLSL inlines this +// trivially so there's no function-call overhead at runtime. +bool tryRenderCell(int cx, int cy, vec2 worldPx, Footprint fp, out vec4 outColor) { + int mapW = int(uMapSize.x); + int mapH = int(uMapSize.y); + if (cx < 0 || cx >= mapW || cy < 0 || cy >= mapH) { + return false; + } + + // the cell's tile does not reach the fragment: known before anything + // is fetched + vec2 inTile = (worldPx - tileOrigin(cx, cy)) / uTileSize; + if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { + return false; + } + + // `texelFetch` skips filtering — the 8-bit channel values come back + // as normalized floats, decoded to byte ints below + vec4 cellF = texelFetch(uTileIndex, ivec2(cx, cy), 0); + uvec4 cell = uvec4(cellF * 255.0 + 0.5); + int firstGid = int(uGidRange.x); + int gid = int(cell.r) | (int(cell.g) << 8); + if (gid < firstGid || gid > int(uGidRange.y)) { + return false; + } + + // flip mask + axis-swap trick: see TMX shader-path flip spec. + // AD performs a transpose; with AD set, H and V swap their effective + // axes (matches the legacy `buildFlipTransform`). + int flipMask = int(cell.b); + float flipH = float(flipMask & 1); + float flipV = float((flipMask >> 1) & 1); + float flipAD = float((flipMask >> 2) & 1); + inTile = mix(inTile, inTile.yx, flipAD); + float effH = mix(flipH, flipV, flipAD); + float effV = mix(flipV, flipH, flipAD); + inTile.x = mix(inTile.x, 1.0 - inTile.x, effH); + inTile.y = mix(inTile.y, 1.0 - inTile.y, effV); + + int localId = gid - firstGid; + + // animation: if the tileset has animated tiles, swap the local id for + // its current frame's id via the lookup texture (CPU updates the + // lookup in lockstep with `tileset.update(dt)`). + if (uAnimSize > 0) { + vec4 animF = texelFetch(uAnimLookup, ivec2(localId, 0), 0); + uvec4 animTexel = uvec4(animF * 255.0 + 0.5); + localId = int(animTexel.r) | (int(animTexel.g) << 8); + } + + // the tile's place in the atlas, in whole numbers: a float division + // may land a hair under the row it should + int cols = int(uTilesetCols.x); + int row = localId / cols; + // a tile the tileset's image does not hold (an image cropped since the + // map was saved): nothing to draw + if (row >= int(uTilesetCols.y)) { + return false; + } + vec2 tileOriginPx = uTilesetMargin.xy + + vec2(float(localId - row * cols), float(row)) * (uTileSize + uTilesetMargin.zw); + vec2 texelPx = tileOriginPx + inTile * uTileSize; + texelPx = clamp(texelPx, tileOriginPx + fp.inset, tileOriginPx + uTileSize - fp.inset); + + vec4 sampled = textureGrad(uSampler, texelPx * uInvTilesetSize, fp.gradX, fp.gradY); + if (sampled.a <= 0.0) { + return false; + } + outColor = sampled; + return true; +} + +// A fragment is opaque from this alpha on: nothing behind it would change a +// channel by half a step of 8 bits +const float OPAQUE = 0.998; + +// Put a texel behind what a fragment has so far, both premultiplied: what +// drawing the tiles back to front, each over the last, comes to, taken from +// the front. Returns whether the fragment is opaque now, and nothing behind +// it can show +bool under(inout vec4 color, vec4 texel) { + color += (1.0 - color.a) * texel; + return color.a >= OPAQUE; +} + +// the layer's opacity and tint applied to a texel. The texel is +// premultiplied, as the blending expects: the opacity scales its color as +// well as its alpha. The tint colors it; its alpha is not an opacity, as on +// a sprite +vec4 layerColor(vec4 texel) { + return texel * uOpacity * vec4(uTint.rgb, 1.0); +} + diff --git a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.vert b/packages/melonjs/src/video/webgl/shaders/tmxlayer.vert similarity index 100% rename from packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.vert rename to packages/melonjs/src/video/webgl/shaders/tmxlayer.vert diff --git a/packages/melonjs/src/video/webgl/webgl_renderer.js b/packages/melonjs/src/video/webgl/webgl_renderer.js index 2e19c26cb6..6afeb19b87 100644 --- a/packages/melonjs/src/video/webgl/webgl_renderer.js +++ b/packages/melonjs/src/video/webgl/webgl_renderer.js @@ -24,6 +24,7 @@ import { import AmbientLightEffect from "../effects/ambientlight.js"; import BlendEffect from "../effects/blendEffect.js"; import RadialGradientEffect from "../effects/radialGradient.js"; +import { TMX_ORIENTATIONS } from "../gpu/tmxlayer_orientation.js"; import { Gradient } from "../gradient.js"; import Renderer from "./../renderer.js"; import RenderTargetPool from "../rendertarget/render_target_pool.js"; @@ -46,7 +47,7 @@ import PrimitiveBatcher from "./batchers/primitive_batcher"; import QuadBatcher from "./batchers/quad_batcher"; import { markContextLost } from "./buffer/deletable.js"; import { createLightUniformScratch, packLights } from "./lighting/pack.ts"; -import OrthogonalTMXLayerGPURenderer from "./renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "./renderers/tmxlayer/tmxlayer.js"; import { WebGLTextureStore } from "./texture/store.js"; import { resolveMaxTextures } from "./utils/maxtextures.js"; import { getMaxShaderPrecision } from "./utils/precision.js"; @@ -520,6 +521,7 @@ export default class WebGLRenderer extends Renderer { // this backend renders TMX tile layers on the GPU (see TMXLayer) this.supportsShaderTileLayers = true; + this.maxTextureSize = this.gl.getParameter(this.gl.MAX_TEXTURE_SIZE); // this backend keeps mesh geometry resident on the GPU, so meshes can // hand it their model matrix instead of pre-transformed vertices @@ -1341,7 +1343,9 @@ export default class WebGLRenderer extends Renderer { // drop every per-layer GPU tilemap texture — tile layers churn on // game reset and we'd otherwise leak a WebGLTexture per layer // across each level transition - this._orthogonalTMXGPURenderer?.reset(); + this._tmxGPURenderers?.forEach((gpu) => { + gpu?.reset(); + }); // initial viewport size this.setViewport(); @@ -1430,7 +1434,7 @@ export default class WebGLRenderer extends Renderer { * @ignore * @internal */ - this._orthogonalTMXGPURenderer = undefined; + this._tmxGPURenderers = undefined; } } @@ -1446,7 +1450,9 @@ export default class WebGLRenderer extends Renderer { drawTileLayer(layer, rect) { if (layer.renderMode === "shader") { const gpu = this._getTMXGPURendererFor(layer.orientation); - if (gpu !== undefined) { + // a layer asked for this path after it was found unfit for it + // is still drawn, tile by tile + if (gpu !== undefined && gpu.orientation.draws(layer)) { gpu.draw(layer, rect); return; } @@ -1487,30 +1493,31 @@ export default class WebGLRenderer extends Renderer { * @internal */ _getTMXGPURendererFor(orientation) { - if (orientation === "orthogonal") { - if (this._orthogonalTMXGPURenderer === undefined) { - try { - this._orthogonalTMXGPURenderer = new OrthogonalTMXLayerGPURenderer( - this, - ); - } catch (err) { - // shader compile / link failure on this driver — disable - // the GPU path permanently for the rest of the session - // (the next `drawTileLayer` call falls through to the - // legacy renderer). Stored as `null` so subsequent - // attempts short-circuit without re-trying the failing - // compile every frame. - console.warn( - "melonJS: GPU tilemap shader failed to compile, falling back to legacy renderer", - err, - ); - this._orthogonalTMXGPURenderer = null; - } + if (TMX_ORIENTATIONS[orientation] === undefined) { + // an oblique map: drawn by the legacy renderer + return undefined; + } + this._tmxGPURenderers ??= new Map(); + let gpu = this._tmxGPURenderers.get(orientation); + if (gpu === undefined) { + try { + gpu = new TMXLayerGPURenderer(this, orientation); + } catch (err) { + // shader compile / link failure on this driver — disable + // the GPU path permanently for the rest of the session + // (the next `drawTileLayer` call falls through to the + // legacy renderer). Stored as `null` so subsequent + // attempts short-circuit without re-trying the failing + // compile every frame. + console.warn( + "melonJS: GPU tilemap shader failed to compile, falling back to legacy renderer", + err, + ); + gpu = null; } - return this._orthogonalTMXGPURenderer || undefined; + this._tmxGPURenderers.set(orientation, gpu); } - // isometric / staggered / hexagonal: phase 2 - return undefined; + return gpu || undefined; } /** diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js similarity index 71% rename from packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js rename to packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js index bd861bc1ec..868f2d0e63 100644 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js @@ -1,6 +1,15 @@ import VertexArrayBuffer from "../../../buffer/vertex.js"; import { transformQuadCorners } from "../../../gpu/quadcorners.ts"; -import tmxLayerWGSL from "../../shaders/tmxlayer.wgsl"; +import { + atlasColumns, + atlasRows, + createRegion, + TMX_ORIENTATIONS, +} from "../../../gpu/tmxlayer_orientation.js"; +import tmxLayerCommonWGSL from "../../shaders/tmxlayer-common.wgsl"; +import isometricWGSL from "../../shaders/tmxlayer-isometric.wgsl"; +import orthogonalWGSL from "../../shaders/tmxlayer-orthogonal.wgsl"; +import staggeredWGSL from "../../shaders/tmxlayer-staggered.wgsl"; /** * additional imports for TypeScript @@ -9,20 +18,36 @@ import tmxLayerWGSL from "../../shaders/tmxlayer.wgsl"; */ /** - * byte size of the TMXUniforms block (see tmxlayer.wgsl): ten vec2f (80) + * byte size of the TMXUniforms block (see tmxlayer-common.wgsl): ten vec2f (80) * interleaved with one vec4f at a 16-aligned offset, two f32, the vec4f - * tint (ending at 112) and the mipmap flag, rounded up to the block's - * 16-byte alignment → 128 bytes + * tint (ending at 112), the mipmap flag (112), and a staggered map's mode + * (116) and two lengths (a vec2f at 120) → 128 bytes, a multiple of the + * block's 16-byte alignment * @ignore * @internal */ const TMX_UNIFORM_SIZE = 128; +// each orientation's part of the shader module, appended to the shared one +const ORIENTATION_WGSL = { + orthogonal: orthogonalWGSL, + isometric: isometricWGSL, + // one geometry: a hexagonal map's cells are longer, no more + staggered: staggeredWGSL, + hexagonal: staggeredWGSL, +}; + /** - * GPU-accelerated renderer for orthogonal TMX tile layers on the WebGPU - * backend — the WGSL realization of the WebGL shader tile path: the - * visible region draws as ONE quad per tileset, with the fragment shader - * sampling a per-layer GID index texture and the tileset atlas. + * GPU-accelerated renderer for TMX tile layers on the WebGPU backend, the + * part every map orientation shares — the WGSL realization of the WebGL + * shader tile path: the visible region draws as ONE quad per tileset, with + * the fragment shader sampling a per-layer GID index texture and the + * tileset atlas. + * + * There is one of these per map orientation drawn (see `TMX_ORIENTATIONS` + * in `gpu/tmxlayer_orientation.js`). Two things differ from one to the next: + * its part of the shader (where a cell's tile is, and which cells can cover + * a fragment), and the part of a layer a view shows. * * Structure mirrors the WebGL twin; the mechanisms are the backend's: * - the GID index / animation lookup are renderer-owned `rgba8unorm` @@ -33,17 +58,34 @@ const TMX_UNIFORM_SIZE = 128; * dynamic offset (each tileset pass gets its own region, per the * queue-write-before-draws ordering) * - the tileset atlas rides the standard texture-store material path - * - pipelines flow through the registered `tmxlayer.wgsl` family, keyed - * on the frame's blend/stencil state like every other draw + * - pipelines flow through the registered shader family (the shared + * module text plus the orientation's), keyed on the frame's blend/stencil + * state like every other draw * @ignore * @internal */ -export default class OrthogonalTMXLayerGPURenderer { +export default class TMXLayerGPURenderer { /** * @param {WebGPURenderer} renderer - the WebGPU renderer instance + * @param {string} orientation - the map orientation to draw, one of + * `TMX_ORIENTATIONS` */ - constructor(renderer) { + constructor(renderer, orientation) { this.renderer = renderer; + /** + * what differs with the map's orientation (see + * `gpu/tmxlayer_orientation.js`) + * @ignore + * @internal + */ + this.orientation = TMX_ORIENTATIONS[orientation]; + // the bounds of the shader's loops, declared ahead of its own part + const limits = Object.entries(this.orientation.limits) + .map(([name, value]) => { + return `const ${name} : i32 = ${value};`; + }) + .join("\n"); + const wgsl = `${limits}\n${ORIENTATION_WGSL[orientation]}`; const cache = renderer.pipelineCache; // group-3 shape: the uniform block + the two lookup textures @@ -60,7 +102,7 @@ export default class OrthogonalTMXLayerGPURenderer { { binding: 1, visibility: GPUShaderStage.FRAGMENT, texture: {} }, { binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: {} }, ]); - this.key = cache.registerShader(tmxLayerWGSL, { + this.key = cache.registerShader(`${tmxLayerCommonWGSL}\n${wgsl}`, { bindGroupLayouts: [ cache.frameLayout, cache.materialLayout, @@ -98,6 +140,18 @@ export default class OrthogonalTMXLayerGPURenderer { * @type {VertexArrayBuffer|undefined} */ this.vertexScratch = undefined; + /** + * what the orientation's `region` fills + * @ignore + * @internal + */ + this.regionScratch = createRegion(); + /** + * what the orientation's `tilesetsOf` fills, when it needs to + * @ignore + * @internal + */ + this.tilesetsScratch = []; } /** @@ -222,36 +276,18 @@ export default class OrthogonalTMXLayerGPURenderer { } /** - * Draw an orthogonal TMX layer through the shader path. - * @param {TMXLayer} layer - the layer to draw - * @param {object} rect - the visible viewport rect (world coords) + * Start a layer's draw, for the first tileset the view shows something + * of: the quad (shared by every tileset pass), the layer's uniform + * values, the pipeline and the bindings that do not change per tileset. + * @param {TMXLayer} layer - the layer + * @param {object} region - the region of that first tileset + * @returns {object} what the tileset passes need: the pass, the index + * texture's resource and the layer's bind group cache + * @ignore + * @internal */ - draw(layer, rect) { + begin(layer, region) { const renderer = this.renderer; - const tileWidth = layer.tilewidth; - const tileHeight = layer.tileheight; - const cols = layer.cols; - const rows = layer.rows; - - // visible region in tile-coord space - const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); - const startTileY = Math.max(0, Math.floor(rect.pos.y / tileHeight)); - const endTileX = Math.min( - cols, - Math.ceil((rect.pos.x + rect.width) / tileWidth), - ); - const endTileY = Math.min( - rows, - Math.ceil((rect.pos.y + rect.height) / tileHeight), - ); - if (endTileX <= startTileX || endTileY <= startTileY) { - return; - } - - const worldX = startTileX * tileWidth; - const worldY = startTileY * tileHeight; - const worldW = (endTileX - startTileX) * tileWidth; - const worldH = (endTileY - startTileY) * tileHeight; // drain pending sprite vertices under their own pipeline, then use // the quad batcher's frozen machinery (staging layout, index buffer) @@ -267,13 +303,18 @@ export default class OrthogonalTMXLayerGPURenderer { // to its own tiles, so the quad carries no frame // on whole pixels unless `subPixel` is set, as a sprite is: the tiles // move with the quad (see Renderer#_pixelSnapBegin) - const snapped = renderer._pixelSnapBegin(worldX, worldY, worldW, worldH); + const snapped = renderer._pixelSnapBegin( + region.x, + region.y, + region.width, + region.height, + ); const corners = transformQuadCorners( renderer.currentTransform, - worldX, - worldY, - worldW, - worldH, + region.x, + region.y, + region.width, + region.height, renderer.currentDepth, ); renderer._pixelSnapEnd(snapped); @@ -307,14 +348,10 @@ export default class OrthogonalTMXLayerGPURenderer { // layer-level uniform values (identical for every tileset pass) const scratch = this.uniformScratch; - scratch[0] = cols; - scratch[1] = rows; - scratch[2] = tileWidth; - scratch[3] = tileHeight; - scratch[18] = startTileX; - scratch[19] = startTileY; - scratch[20] = endTileX - startTileX; - scratch[21] = endTileY - startTileY; + scratch[0] = layer.cols; + scratch[1] = layer.rows; + scratch[2] = layer.tilewidth; + scratch[3] = layer.tileheight; // the layer's opacity, and the fade of everything it is drawn in: // set on the renderer by the layer's preDraw scratch[22] = renderer.getGlobalAlpha(); @@ -346,19 +383,40 @@ export default class OrthogonalTMXLayerGPURenderer { ); pass.setIndexBuffer(batcher.indexBuffer, "uint32"); - // one pass per tileset — the GID-range guard hides other tilesets - const tilesets = layer.tilesets.tilesets; let layerGroups = this.bindGroups.get(layer); if (layerGroups === undefined) { layerGroups = new Map(); this.bindGroups.set(layer, layerGroups); } + return { pass, resource, layerGroups }; + } + + /** + * Draw a TMX layer through the shader path. + * @param {TMXLayer} layer - the layer to draw + * @param {object} rect - the visible viewport rect (world coords) + */ + draw(layer, rect) { + const renderer = this.renderer; + const device = renderer.device; + const scratch = this.uniformScratch; + const region = this.regionScratch; + let begun; + + // one pass per tileset — the GID-range guard hides other tilesets + const tilesets = this.orientation.tilesetsOf(layer, this.tilesetsScratch); for (let i = 0; i < tilesets.length; i++) { const tileset = tilesets[i]; // eligibility should have excluded these — guard defensively if (tileset.isCollection || tileset.image === undefined) { continue; } + // the view shows nothing of this tileset's tiles + if (!this.orientation.region(layer, rect, tileset, region)) { + continue; + } + begun ??= this.begin(layer, region); + const { pass, resource, layerGroups } = begun; const tsW = tileset.tilewidth; const tsH = tileset.tileheight; @@ -366,18 +424,12 @@ export default class OrthogonalTMXLayerGPURenderer { const spacing = tileset.spacing; const atlasW = tileset.image.width; const atlasH = tileset.image.height; - const atlasCols = Math.max( - 1, - Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), - ); - const atlasRows = Math.max( - 1, - Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), - ); + const atlasCols = atlasColumns(tileset); + const atlasRowCount = atlasRows(tileset); const animEntry = this.getOrUpdateAnimLookup( tileset, - atlasCols * atlasRows, + atlasCols * atlasRowCount, ); // every declared binding needs a view — reuse the index texture // as the filler (never read: uAnimSize stays 0) @@ -386,10 +438,10 @@ export default class OrthogonalTMXLayerGPURenderer { // per-tileset uniform values + snapshot into the arena scratch[4] = tsW; scratch[5] = tsH; - scratch[6] = Math.max(0, Math.ceil(tsW / tileWidth) - 1); - scratch[7] = Math.max(0, Math.ceil(tsH / tileHeight) - 1); + scratch[6] = region.overflowX; + scratch[7] = region.overflowY; scratch[8] = atlasCols; - scratch[9] = atlasRows; + scratch[9] = atlasRowCount; scratch[10] = 1 / atlasW; scratch[11] = 1 / atlasH; scratch[12] = margin; @@ -398,6 +450,10 @@ export default class OrthogonalTMXLayerGPURenderer { scratch[15] = spacing; scratch[16] = tileset.firstgid; scratch[17] = tileset.lastgid; + scratch[18] = region.startX; + scratch[19] = region.startY; + scratch[20] = region.sizeX; + scratch[21] = region.sizeY; scratch[23] = animEntry ? animEntry.tileCount : 0; // the "linear-mipmap" filter, on a linear tileset: the binding // below asks the store for a chain, and the shader keeps each @@ -409,13 +465,18 @@ export default class OrthogonalTMXLayerGPURenderer { const mipmaps = filter === "linear" && renderer.wantsMipmaps(tileset.texture); scratch[28] = mipmaps ? 1 : 0; - const region = renderer.effectUniformArena.alloc( + // a staggered or hexagonal map's (0 on the others, which have + // no use for them) + scratch[29] = region.staggerMode; + scratch[30] = region.staggerX; + scratch[31] = region.staggerY; + const uniforms = renderer.effectUniformArena.alloc( TMX_UNIFORM_SIZE, device.limits?.minUniformBufferOffsetAlignment ?? 256, ); device.queue.writeBuffer( - region.buffer, - region.offset, + uniforms.buffer, + uniforms.offset, scratch.buffer, 0, TMX_UNIFORM_SIZE, @@ -426,11 +487,11 @@ export default class OrthogonalTMXLayerGPURenderer { let cached = layerGroups.get(tileset); if ( cached === undefined || - cached.pageLabel !== region.buffer.label || + cached.pageLabel !== uniforms.buffer.label || cached.animView !== animView ) { cached = { - pageLabel: region.buffer.label, + pageLabel: uniforms.buffer.label, animView, bindGroup: device.createBindGroup({ label: "melonJS tmx binding", @@ -439,7 +500,7 @@ export default class OrthogonalTMXLayerGPURenderer { { binding: 0, resource: { - buffer: region.buffer, + buffer: uniforms.buffer, size: TMX_UNIFORM_SIZE, }, }, @@ -455,7 +516,7 @@ export default class OrthogonalTMXLayerGPURenderer { 1, renderer.textureStore.getBinding(tileset.texture, { mipmaps }), ); - pass.setBindGroup(3, cached.bindGroup, [region.offset]); + pass.setBindGroup(3, cached.bindGroup, [uniforms.offset]); pass.drawIndexed(6); } } diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl similarity index 61% rename from packages/melonjs/src/video/webgpu/shaders/tmxlayer.wgsl rename to packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index d892c921bb..4e7f1f6151 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -1,12 +1,16 @@ -// melonJS WebGPU orthogonal TMX layer shader — the WGSL port of -// orthogonal-tmxlayer.vert/frag (WebGL2). +// melonJS WebGPU TMX layer shader — the WGSL port of tmxlayer.vert and +// tmxlayer-common.frag (WebGL2): the part every map orientation shares. An +// orientation's own file follows it in the module (tmxlayer-orthogonal.wgsl, +// tmxlayer-isometric.wgsl, tmxlayer-staggered.wgsl for staggered and +// hexagonal maps) and gives the two things that differ: where a +// cell's tile is drawn (`tileOrigin`), and which cells could cover a +// fragment, front to back (`fragment_main`). // -// Per fragment: recover the world-pixel position from the quad UV, walk -// candidate cells (the geometric cell, plus cells whose oversized -// bottom-aligned tiles could reach this fragment), fetch GIDs from the -// per-layer index texture, and sample the tileset atlas at the correct -// sub-region. One draw per tileset — the GID-range guard discards cells -// belonging to other tilesets. +// Per fragment: recover the fragment's position in the layer from the quad +// UV, walk the cells whose tile could cover it, the one drawn last first, +// fetch GIDs from the per-layer index texture, and sample the tileset atlas +// at the correct sub-region. One draw per tileset — the GID-range guard +// discards cells belonging to other tilesets. // // Index texture encoding (rgba8unorm, one cell per texel): // R = GID low byte, G = GID high byte, B = flip mask (H|V<<1|AD<<2) @@ -14,9 +18,9 @@ // local id, same R/G byte-pair encoding. // // textureLoad replaces texelFetch (byte-exact, no sampler); the atlas -// sample uses textureSampleLevel — the sample sites sit in non-uniform -// control flow, where implicit-derivative sampling is not allowed (WGSL -// uniformity rule; atlases are single-level so the output is identical). +// sample uses textureSampleGrad, with derivatives taken once per fragment — +// the sample sites sit in non-uniform control flow, where +// implicit-derivative sampling is not allowed (WGSL uniformity rule). struct FrameUniforms { projection : mat4x4, @@ -27,7 +31,7 @@ struct TMXUniforms { uMapSize : vec2f, uCellSize : vec2f, uTileSize : vec2f, - uOverflow : vec2f, + uOverflow : vec2f, // how far a tile reaches past its cell (see each fragment_main) uTilesetCols : vec2f, uInvTilesetSize : vec2f, uTilesetMargin : vec4f, // (marginX, marginY, spacingX, spacingY) @@ -38,6 +42,10 @@ struct TMXUniforms { uAnimSize : f32, // entries in the anim lookup, 0 when disabled uTint : vec4f, uMipmaps : f32, // 1 under the "linear-mipmap" filter, else 0 + // a staggered or hexagonal map's (see tmxlayer-staggered.wgsl); they + // fill what was the block's padding + uStaggerMode : f32, // 1 when the stagger axis is x, + 2 when the index is even + uStagger : vec2f, // (columnwidth, rowheight) }; @group(0) @binding(0) var uFrame : FrameUniforms; @@ -69,28 +77,40 @@ fn vertex_main( return out; } -const MAX_OVERFLOW : i32 = 4; - struct CellSample { hit : bool, color : vec4f, } +// What sampling the atlas needs of a fragment, the same for every cell +// tried for it: taken once, in `fragment_main`, where control flow is still +// uniform (see `footprint` in tmxlayer-common.frag). +struct Footprint { + gradX : vec2f, + gradY : vec2f, + inset : vec2f, +} + +fn footprint(worldPx : vec2f) -> Footprint { + let dWorldX = dpdx(worldPx); + let dWorldY = dpdy(worldPx); + // keep the sample half a texel of the level it reads inside the tile, + // so filtering never blends in the next tile of the tileset, or the + // spacing around it, along the tile's edge: a seam between two tiles + // (never past the tile's centre). Without mipmaps that level is the + // full-size art; under them, the deepest level read is the next one up + // once the level is past a whole number, by more than a rounding error: + // drawn at its size, the derivatives are 1 give or take a few ulps, and + // level 0 it is. Sampled nearest, the sample stays in the same texel + let lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); + let inset = min(vec2f(0.5 * exp2(lod * tmx.uMipmaps)), tmx.uTileSize * 0.5); + return Footprint(dWorldX * tmx.uInvTilesetSize, dWorldY * tmx.uInvTilesetSize, inset); +} + // Try to render the tile at cell (cx, cy) for the current fragment — // identical logic to the GLSL tryRenderCell, returning a hit/color pair // (WGSL has no out parameters). -// `dWorldX` / `dWorldY`: the world position's screen derivatives, taken in -// `fragment_main` while control flow is still uniform. The atlas coordinate -// jumps from one tile to the next, so its own derivatives would pick a tiny -// mip level along every tile edge; a texel of the atlas is a world pixel, -// so the world's derivatives are the atlas's. -fn tryRenderCell( - cx : i32, - cy : i32, - worldPx : vec2f, - dWorldX : vec2f, - dWorldY : vec2f, -) -> CellSample { +fn tryRenderCell(cx : i32, cy : i32, worldPx : vec2f, fp : Footprint) -> CellSample { var miss : CellSample; miss.hit = false; miss.color = vec4f(0.0); @@ -101,6 +121,13 @@ fn tryRenderCell( return miss; } + // the cell's tile does not reach the fragment (where it is drawn from + // is the orientation's own): known before anything is fetched + var inTile = (worldPx - tileOrigin(cx, cy)) / tmx.uTileSize; + if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { + return miss; + } + // byte-exact read: rgba8unorm texel back to byte ints let cellF = textureLoad(uTileIndex, vec2i(cx, cy), 0); let cell = vec4u(cellF * 255.0 + 0.5); @@ -110,16 +137,6 @@ fn tryRenderCell( return miss; } - // oversized tiles are bottom-aligned vertically, left-aligned horizontally - let tileWorldOrigin = vec2f( - f32(cx) * tmx.uCellSize.x, - (f32(cy) + 1.0) * tmx.uCellSize.y - tmx.uTileSize.y, - ); - var inTile = (worldPx - tileWorldOrigin) / tmx.uTileSize; - if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { - return miss; - } - // flip mask + axis-swap trick: AD transposes; with AD set, H and V // swap their effective axes (matches the legacy buildFlipTransform) let flipMask = i32(cell.b); @@ -142,32 +159,28 @@ fn tryRenderCell( localId = i32(animTexel.r) | (i32(animTexel.g) << 8u); } - let row = floor(f32(localId) / tmx.uTilesetCols.x); - let col = f32(localId) - row * tmx.uTilesetCols.x; + // the tile's place in the atlas, in whole numbers: a float division + // may land a hair under the row it should + let cols = i32(tmx.uTilesetCols.x); + let row = localId / cols; + // a tile the tileset's image does not hold (an image cropped since the + // map was saved): nothing to draw + if (row >= i32(tmx.uTilesetCols.y)) { + return miss; + } let tileOriginPx = tmx.uTilesetMargin.xy - + vec2f(col, row) * (tmx.uTileSize + tmx.uTilesetMargin.zw); + + vec2f(f32(localId - row * cols), f32(row)) * (tmx.uTileSize + tmx.uTilesetMargin.zw); var texelPx = tileOriginPx + inTile * tmx.uTileSize; - // keep the sample half a texel of the level it reads inside the tile, - // so filtering never blends in the next tile of the tileset, or the - // spacing around it, along the tile's edge: a seam between two tiles - // (never past the tile's centre). Without mipmaps that level is the - // full-size art; under them, the deepest level read is the next one up - // once the level is past a whole number, by more than a rounding error: - // drawn at its size, the derivatives are 1 give or take a few ulps, and - // level 0 it is. Sampled nearest, the sample stays in the same texel - let lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); - let inset = min(vec2f(0.5 * exp2(lod * tmx.uMipmaps)), tmx.uTileSize * 0.5); - texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + tmx.uTileSize - inset); - let texelUV = texelPx * tmx.uInvTilesetSize; + texelPx = clamp(texelPx, tileOriginPx + fp.inset, tileOriginPx + tmx.uTileSize - fp.inset); // explicit gradients, so the sample is legal here (non-uniform control // flow); a sampler without mipmaps clamps to level 0, as before let sampled = textureSampleGrad( uTexture, uSampler, - texelUV, - dWorldX * tmx.uInvTilesetSize, - dWorldY * tmx.uInvTilesetSize, + texelPx * tmx.uInvTilesetSize, + fp.gradX, + fp.gradY, ); if (sampled.a <= 0.0) { return miss; @@ -178,6 +191,19 @@ fn tryRenderCell( return out; } +// A fragment is opaque from this alpha on: nothing behind it would change a +// channel by half a step of 8 bits +const OPAQUE : f32 = 0.998; + +// Put a texel behind what a fragment has so far, both premultiplied: what +// drawing the tiles back to front, each over the last, comes to, taken from +// the front. Returns whether the fragment is opaque now, and nothing behind +// it can show +fn under(color : ptr, texel : vec4f) -> bool { + *color += (1.0 - (*color).a) * texel; + return (*color).a >= OPAQUE; +} + // the layer's opacity and tint applied to a texel. The texel is // premultiplied, as the blending expects: the opacity scales its color as // well as its alpha. The tint colors it; its alpha is not an opacity, as on @@ -186,57 +212,3 @@ fn layerColor(texel : vec4f) -> vec4f { return texel * tmx.uOpacity * vec4f(tmx.uTint.rgb, 1.0); } -@fragment -fn fragment_main(in : VSOut) -> @location(0) vec4f { - let tileCoord = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; - let geomCell = floor(tileCoord); - let worldPx = tileCoord * tmx.uCellSize; - let dWorldX = dpdx(worldPx); - let dWorldY = dpdy(worldPx); - - let gx = i32(geomCell.x); - let gy = i32(geomCell.y); - let overflowX = i32(tmx.uOverflow.x + 0.5); - let overflowY = i32(tmx.uOverflow.y + 0.5); - - var result : CellSample; - - // fast path: tiles fit the cell exactly — only the geometric cell can - // contain this fragment's tile - if (overflowX == 0 && overflowY == 0) { - result = tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY); - if (!result.hit) { - discard; - } - return layerColor(result.color); - } - - // slow path: oversized tiles. Scan candidates dy high→low, dx low→high - // and take the FIRST match — "right-down" render order puts later - // cells on top - var found = false; - for (var idy = 0; idy <= MAX_OVERFLOW; idy++) { - let dy = MAX_OVERFLOW - idy; - if (dy > overflowY) { - continue; - } - for (var dx = 0; dx <= MAX_OVERFLOW; dx++) { - if (dx > overflowX) { - break; - } - let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY); - if (candidate.hit) { - result = candidate; - found = true; - break; - } - } - if (found) { - break; - } - } - if (!found) { - discard; - } - return layerColor(result.color); -} diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl new file mode 100644 index 0000000000..e948a3263e --- /dev/null +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl @@ -0,0 +1,67 @@ +// The isometric part of the TMX layer shader: appended to +// tmxlayer-common.wgsl. The WGSL port of isometric-tmxlayer.frag, where the +// cell maths is explained. +// +// A fragment's position is in the layer's pixels, measured across from the +// map's top corner. With u = col - row and s = col + row, the cells whose +// tile image covers it are a run of `u` and a run of `s`, of the lengths in +// `uOverflow`, scanned s high→low then u high→low: the reverse of the order +// the CPU renderer draws in, each texel put behind the ones before it until +// the fragment is opaque. + +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in gpu/tmxlayer_orientation.js). + +// the left of the cell's diamond, and as far above its bottom as the tile +// is tall +fn tileOrigin(cx : i32, cy : i32) -> vec2f { + return vec2f( + f32(cx - cy - 1) * tmx.uCellSize.x * 0.5, + f32(cx + cy + 2) * tmx.uCellSize.y * 0.5 - tmx.uTileSize.y, + ); +} + +@fragment +fn fragment_main(in : VSOut) -> @location(0) vec4f { + let worldPx = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; + let fp = footprint(worldPx); + + let halfCell = tmx.uCellSize * 0.5; + // the rightmost and the lowest cell whose image can reach the fragment + let uMax = i32(floor(worldPx.x / halfCell.x)) + 1; + let sMax = i32(floor((worldPx.y + tmx.uTileSize.y) / halfCell.y)) - 2; + let spanU = i32(tmx.uOverflow.x + 0.5); + let spanS = i32(tmx.uOverflow.y + 0.5); + + var result = vec4f(0.0); + var opaque = false; + // rows past the map's last one, or before its first, hold no cell: most + // of a fragment's run, along the bottom of the map and above its top + let sLast = i32(tmx.uMapSize.x) + i32(tmx.uMapSize.y) - 2; + for (var j = max(sMax - sLast, 0); j < MAX_ROWS; j++) { + let s = sMax - j; + if (j >= spanS || s < 0) { + break; + } + // the first u of this row's parity, then every other one + var u = uMax - ((uMax + s) & 1); + for (var i = 0; i < MAX_COLS; i++) { + if (uMax - u >= spanU) { + break; + } + let candidate = tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, fp); + if (candidate.hit && under(&result, candidate.color)) { + opaque = true; + break; + } + u -= 2; + } + if (opaque) { + break; + } + } + if (result.a <= 0.0) { + discard; + } + return layerColor(result); +} diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl new file mode 100644 index 0000000000..d2ac310b81 --- /dev/null +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl @@ -0,0 +1,67 @@ +// The orthogonal part of the TMX layer shader: appended to +// tmxlayer-common.wgsl. The WGSL port of orthogonal-tmxlayer.frag. +// +// `uOverflow` is how many cells a tile reaches past its own, across and up. + +// `MAX_OVERFLOW`, the bound of both loops, is declared ahead of this file +// (see `TMX_ORIENTATIONS` in gpu/tmxlayer_orientation.js). + +// a tile is bottom-aligned on its cell, and left-aligned +fn tileOrigin(cx : i32, cy : i32) -> vec2f { + return vec2f( + f32(cx) * tmx.uCellSize.x, + (f32(cy) + 1.0) * tmx.uCellSize.y - tmx.uTileSize.y, + ); +} + +@fragment +fn fragment_main(in : VSOut) -> @location(0) vec4f { + let tileCoord = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; + let geomCell = floor(tileCoord); + let worldPx = tileCoord * tmx.uCellSize; + let fp = footprint(worldPx); + + let gx = i32(geomCell.x); + let gy = i32(geomCell.y); + let overflowX = i32(tmx.uOverflow.x + 0.5); + let overflowY = i32(tmx.uOverflow.y + 0.5); + + // fast path: tiles fit the cell exactly — only the geometric cell can + // contain this fragment's tile + if (overflowX == 0 && overflowY == 0) { + let only = tryRenderCell(gx, gy, worldPx, fp); + if (!only.hit) { + discard; + } + return layerColor(only.color); + } + + // slow path: oversized tiles. Scan candidates dy high→low, dx low→high, + // front to back — "right-down" render order puts later cells on top — + // each texel behind the ones before it, until the fragment is opaque + var result = vec4f(0.0); + var opaque = false; + for (var idy = 0; idy <= MAX_OVERFLOW; idy++) { + let dy = MAX_OVERFLOW - idy; + if (dy > overflowY) { + continue; + } + for (var dx = 0; dx <= MAX_OVERFLOW; dx++) { + if (dx > overflowX) { + break; + } + let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, fp); + if (candidate.hit && under(&result, candidate.color)) { + opaque = true; + break; + } + } + if (opaque) { + break; + } + } + if (result.a <= 0.0) { + discard; + } + return layerColor(result); +} diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl new file mode 100644 index 0000000000..90d06b8939 --- /dev/null +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl @@ -0,0 +1,83 @@ +// The staggered and hexagonal part of the TMX layer shader: appended to +// tmxlayer-common.wgsl. The WGSL port of staggered-tmxlayer.frag, where the +// geometry is explained. +// +// Cells stand in lines, `tmx.uStagger.y` (a row height) apart down the +// screen; in a line they are two `tmx.uStagger.x` (a column width) apart, +// and every other line is shifted by one. With the stagger axis "y" a line +// is a row of the map; with "x" a row is two lines, its columns alternating +// between them. +// +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in gpu/tmxlayer_orientation.js). + +// whether a line is one of the shifted ones: 1, or 0 +fn shifted(line : i32) -> i32 { + return (line & 1) ^ (i32(tmx.uStaggerMode) >> 1u); +} + +// the bottom left of the cell's box, less the tile's height +fn tileOrigin(cx : i32, cy : i32) -> vec2f { + let staggerX = (i32(tmx.uStaggerMode) & 1) == 1; + var line = cy; + var x = f32(2 * cx + shifted(cy)) * tmx.uStagger.x; + if (staggerX) { + line = 2 * cy + shifted(cx); + x = f32(cx) * tmx.uStagger.x; + } + return vec2f(x, f32(line) * tmx.uStagger.y + tmx.uCellSize.y - tmx.uTileSize.y); +} + +@fragment +fn fragment_main(in : VSOut) -> @location(0) vec4f { + let worldPx = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; + let fp = footprint(worldPx); + + let staggerX = (i32(tmx.uStaggerMode) & 1) == 1; + let spacing = tmx.uStagger.x * 2.0; + // the lowest line whose image can reach the fragment + let lineMax = i32(floor((worldPx.y - tmx.uCellSize.y + tmx.uTileSize.y) / tmx.uStagger.y)); + let spanK = i32(tmx.uOverflow.x + 0.5); + let spanL = i32(tmx.uOverflow.y + 0.5); + // lines past the map's last one, or before its first, hold no cell + var lineLast = i32(tmx.uMapSize.y) - 1; + if (staggerX) { + lineLast = 2 * i32(tmx.uMapSize.y) - 1; + } + + var result = vec4f(0.0); + var opaque = false; + for (var j = max(lineMax - lineLast, 0); j < MAX_ROWS; j++) { + let line = lineMax - j; + if (j >= spanL || line < 0) { + break; + } + let shift = shifted(line); + // the rightmost cell of the line whose image can reach the fragment + let kMax = i32(floor((worldPx.x - f32(shift) * tmx.uStagger.x) / spacing)); + for (var i = 0; i < MAX_COLS; i++) { + if (i >= spanK) { + break; + } + let k = kMax - i; + var cx = k; + var cy = line; + if (staggerX) { + cx = 2 * k + shift; + cy = line >> 1u; + } + let candidate = tryRenderCell(cx, cy, worldPx, fp); + if (candidate.hit && under(&result, candidate.color)) { + opaque = true; + break; + } + } + if (opaque) { + break; + } + } + if (result.a <= 0.0) { + discard; + } + return layerColor(result); +} diff --git a/packages/melonjs/src/video/webgpu/webgpu_renderer.js b/packages/melonjs/src/video/webgpu/webgpu_renderer.js index 5d506dcebf..d2c25cdb55 100644 --- a/packages/melonjs/src/video/webgpu/webgpu_renderer.js +++ b/packages/melonjs/src/video/webgpu/webgpu_renderer.js @@ -25,6 +25,7 @@ import BlendEffect from "../effects/blendEffect.js"; import { meshHostingBlocker } from "../effects/mesh_splice.js"; import RadialGradientEffect from "../effects/radialGradient.js"; import { frameClampMode } from "../gpu/frame.js"; +import { TMX_ORIENTATIONS } from "../gpu/tmxlayer_orientation.js"; import { Gradient } from "../gradient.js"; import Renderer from "../renderer.js"; import RenderTargetPool from "../rendertarget/render_target_pool.js"; @@ -54,7 +55,7 @@ import WebGPUBufferArena from "./buffer/arena.js"; import WebGPUUniformRing from "./buffer/uniformring.js"; import { buildEffectGPU } from "./effect_binding.js"; import WebGPUPipelineCache, { DEPTH_STENCIL_FORMAT } from "./pipeline/cache.js"; -import OrthogonalTMXLayerGPURenderer from "./renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "./renderers/tmxlayer/tmxlayer.js"; import { COMPRESSION_FEATURES } from "./texture/compressed.js"; import { WebGPUFrameTexture } from "./texture/frametexture.js"; import WebGPUTextureStore from "./texture/store.js"; @@ -198,6 +199,7 @@ export default class WebGPURenderer extends Renderer { this.supportsDepthBuffer = true; this.supportsRetainedMesh = true; this.supportsInstancing = true; + // (`maxTextureSize` is the device's, once there is one: see `init`) // `rgba16float` is renderable, blendable and filterable in core // WebGPU, with no optional feature to request and nothing to probe — // unlike WebGL2, where it takes an extension that some drivers do @@ -213,11 +215,11 @@ export default class WebGPURenderer extends Renderer { this._resolveToneMapping(); // presentation is settled again by init(), once the surface exists this.setHDROutput(this.settings.hdrOutput === true); - // lazy orientation-specific GPU tilemap renderer (device-scoped: - // dropped on device loss, rebuilt on first use) + // lazy orientation-specific GPU tilemap renderers, by orientation + // (device-scoped: dropped on device loss, rebuilt on first use) /** @ignore * @internal */ - this.orthogonalTMXRenderer = undefined; + this.tmxRenderers = new Map(); // create a texture cache /** @@ -554,6 +556,8 @@ export default class WebGPURenderer extends Renderer { return adapter.features.has(feature); }), }); + this.maxTextureSize = + this.device.limits?.maxTextureDimension2D ?? this.maxTextureSize; // identify the driver in the console header, same as the WebGL // backend does through its debug-renderer-info strings. The @@ -2509,16 +2513,20 @@ export default class WebGPURenderer extends Renderer { * @override */ drawTileLayer(layer, rect) { - if (layer.renderMode === "shader" && layer.orientation === "orthogonal") { + // a layer asked for this path after it was found unfit for it is + // still drawn, tile by tile + if ( + layer.renderMode === "shader" && + TMX_ORIENTATIONS[layer.orientation]?.draws(layer) === true + ) { // device-scoped, lazily (re)built: a device loss rebuilds the // pipeline cache, detected via the epoch stamp - if ( - this.orthogonalTMXRenderer === undefined || - this.orthogonalTMXRenderer.epoch !== this.pipelineCache.epoch - ) { - this.orthogonalTMXRenderer = new OrthogonalTMXLayerGPURenderer(this); + let tmx = this.tmxRenderers.get(layer.orientation); + if (tmx === undefined || tmx.epoch !== this.pipelineCache.epoch) { + tmx = new TMXLayerGPURenderer(this, layer.orientation); + this.tmxRenderers.set(layer.orientation, tmx); } - this.orthogonalTMXRenderer.draw(layer, rect); + tmx.draw(layer, rect); return; } super.drawTileLayer(layer, rect); @@ -4744,8 +4752,8 @@ export default class WebGPURenderer extends Renderer { this.stubTextureView = null; this.currentRenderTarget = null; // tile-path lookup textures died with the device; the epoch check - // in drawTileLayer rebuilds the renderer lazily - this.orthogonalTMXRenderer = undefined; + // in drawTileLayer rebuilds the renderers lazily + this.tmxRenderers.clear(); this.depthTexture = null; this.msaaDepthTexture = null; this.msaaColorTexture = null; @@ -4802,7 +4810,9 @@ export default class WebGPURenderer extends Renderer { this.effectPassDepth = 0; this.customShader = undefined; // level transition: free the per-layer/per-tileset lookup textures - this.orthogonalTMXRenderer?.reset(); + this.tmxRenderers.forEach((tmx) => { + tmx.reset(); + }); super.reset(); // re-init batchers when recovering from a device loss, plain reset // otherwise — the same split as the WebGL restore path. A reset @@ -4867,7 +4877,7 @@ export default class WebGPURenderer extends Renderer { this.pipelineCache?.clear(); this.captureTexture?.destroy(); this.captureTexture = undefined; - this.orthogonalTMXRenderer = undefined; + this.tmxRenderers.clear(); this._renderTargetPool?.destroy(); this._renderTargetPool = null; this.effectUniformArena?.destroy(); diff --git a/packages/melonjs/tests/helpers/mask-effect-checks.js b/packages/melonjs/tests/helpers/mask-effect-checks.js new file mode 100644 index 0000000000..335e83fbcb --- /dev/null +++ b/packages/melonjs/tests/helpers/mask-effect-checks.js @@ -0,0 +1,271 @@ +/** + * What the `MaskEffect` drawing tests share, so that each renderer can run + * them in a spec file of its own: an application with a GPU device takes the + * place of the one before it, and the checks below only need a renderer. + */ +import { expect, it, vi } from "vitest"; +import { Camera2d, Ellipse, MaskEffect, Polygon } from "../../src/index.js"; +import { grabFrame } from "./pixels.js"; + +export const SIZE = 64; + +/** a 32 px disc, radius 14, with a hole of radius 4 at its centre */ +export const discWithHole = () => { + const canvas = document.createElement("canvas"); + canvas.width = canvas.height = 32; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#3a7"; + ctx.beginPath(); + ctx.arc(16, 16, 14, 0, Math.PI * 2); + ctx.fill(); + ctx.globalCompositeOperation = "destination-out"; + ctx.beginPath(); + ctx.arc(16, 16, 4, 0, Math.PI * 2); + ctx.fill(); + return canvas; +}; + +/** an image whose left half only is opaque */ +export const leftHalf = (w = 20, h = 30) => { + const canvas = document.createElement("canvas"); + canvas.width = w; + canvas.height = h; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#f00"; + ctx.fillRect(0, 0, w / 2, h); + return canvas; +}; + +/** a 32 px U: its middle is open to the outside, so not part of it */ +export const uShape = () => { + const canvas = document.createElement("canvas"); + canvas.width = canvas.height = 32; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#000"; + ctx.fillRect(2, 2, 8, 28); + ctx.fillRect(22, 2, 8, 28); + ctx.fillRect(2, 22, 28, 8); + return canvas; +}; + +/** an image whose top left quarter only is opaque */ +export const topLeftQuarter = (w = 20, h = 30) => { + const canvas = document.createElement("canvas"); + canvas.width = w; + canvas.height = h; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#0f0"; + ctx.fillRect(0, 0, w / 2, h / 2); + return canvas; +}; + +/** a unit square that does not surround its origin */ +export const offOrigin = () => { + return new Polygon(0, 0, [ + { x: 0.2, y: 0.2 }, + { x: 1, y: 0.2 }, + { x: 1, y: 1 }, + { x: 0.2, y: 1 }, + ]); +}; + +/** an effect at a given progress (1 = all visible, 0 = all covered) */ +export const at = (camera, progress, options) => { + const effect = new MaskEffect(camera, { + color: "#000", + duration: 1000, + direction: "hide", + ...options, + }); + effect.tween.stop(); + effect.progress.value = progress; + return effect; +}; + +/** + * Draw an effect over a white screen, and return the frame. The frame is + * taken at once, in one piece: an application's own loop draws its next + * frame while a test awaits, over the one a pixel would be read from. + */ +export const drawOverWhite = (renderer, effect) => { + // a frame of its own: a GPU renderer draws nothing outside one + renderer.clear(); + renderer.save(); + renderer.resetTransform(); + renderer.setColor("#ffffff"); + renderer.fillRect(0, 0, SIZE, SIZE); + renderer.restore(); + effect.draw(renderer, SIZE, SIZE); + return grabFrame(renderer); +}; + +/** 1 where a pixel of a frame is covered (dark), 0 where it is visible (light) */ +export const covered = (frame, x, y) => { + return frame.data[(y * frame.width + x) * 4] < 128 ? 1 : 0; +}; + +export const CORNERS = [ + [1, 1], + [SIZE - 2, 1], + [1, SIZE - 2], + [SIZE - 2, SIZE - 2], +]; + +/** the same drawing checks, on one renderer */ +export const drawingChecks = (getRenderer) => { + const camera = new Camera2d(0, 0, SIZE, SIZE); + + it("an image: the scene shows through its silhouette, hole filled in", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.3, { shape: discWithHole() }); + frame = await drawOverWhite(renderer, effect); + // the centre is the image's hole: part of the silhouette, so visible + expect(covered(frame, 32, 32)).toBe(0); + expect(covered(frame, 32, 26)).toBe(0); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(1); + } + effect.destroy(); + }); + + it("an image: covers everything once closed", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0, { shape: discWithHole() }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 32, 32)).toBe(1); + expect(covered(frame, 1, 1)).toBe(1); + effect.destroy(); + }); + + it("an image: just before the end, the whole screen is already uncovered", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.995, { shape: discWithHole() }); + frame = await drawOverWhite(renderer, effect); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + effect.destroy(); + }); + + it("a polygon: just before the end, the whole screen is already uncovered", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const diamond = new Polygon(0, 0, [ + { x: 0, y: -1 }, + { x: 1, y: 0 }, + { x: 0, y: 1 }, + { x: -1, y: 0 }, + ]); + const effect = at(camera, 0.995, { shape: diamond }); + frame = await drawOverWhite(renderer, effect); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + effect.destroy(); + }); + + it("inverted: the silhouette is the cover, the scene shows round it", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.7, { shape: discWithHole(), invert: true }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 32, 32)).toBe(1); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + effect.destroy(); + }); + + it("inverted: covers everything once closed, nothing at the start", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const closed = at(camera, 0, { shape: discWithHole(), invert: true }); + frame = await drawOverWhite(renderer, closed); + expect(covered(frame, 1, 1)).toBe(1); + expect(covered(frame, 32, 32)).toBe(1); + closed.destroy(); + const open = at(camera, 1, { shape: discWithHole(), invert: true }); + frame = await drawOverWhite(renderer, open); + expect(covered(frame, 32, 32)).toBe(0); + open.destroy(); + }); + + it("inverted geometry: an iris filled, growing out of the centre", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.7, { + shape: new Ellipse(0, 0, 1, 1), + invert: true, + }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 32, 32)).toBe(1); + expect(covered(frame, 1, 1)).toBe(0); + effect.destroy(); + }); + + it("an image open in its middle (a U): grows about its thickest part, and still uncovers everything", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + // just after the hide starts, the middle of the screen is still seen + const start = at(camera, 0.99, { shape: uShape() }); + frame = await drawOverWhite(renderer, start); + expect(covered(frame, 32, 32)).toBe(0); + start.destroy(); + // and just before a reveal ends, nothing is covered + const end = at(camera, 0.995, { shape: uShape() }); + frame = await drawOverWhite(renderer, end); + for (const [x, y] of [...CORNERS, [32, 32], [32, 3]]) { + expect(covered(frame, x, y)).toBe(0); + } + end.destroy(); + }); + + it("a polygon off its origin: still uncovers everything, and inverted still covers it", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const open = at(camera, 0.995, { shape: offOrigin() }); + frame = await drawOverWhite(renderer, open); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + open.destroy(); + // the cover nearly grown: the screen is covered by the polygon itself + const cover = at(camera, 0.005, { shape: offOrigin(), invert: true }); + frame = await drawOverWhite(renderer, cover); + for (const [x, y] of [...CORNERS, [32, 32]]) { + expect(covered(frame, x, y)).toBe(1); + } + cover.destroy(); + }); + + it("nearly closed, the image is still drawn, a sliver of the scene in the middle", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.05, { shape: discWithHole() }); + const drawImage = vi.spyOn(renderer, "drawImage"); + frame = await drawOverWhite(renderer, effect); + expect(drawImage).toHaveBeenCalled(); + drawImage.mockRestore(); + expect(covered(frame, 32, 32)).toBe(0); + for (const [x, y] of [...CORNERS, [32, 20], [44, 32]]) { + expect(covered(frame, x, y)).toBe(1); + } + effect.destroy(); + }); + + it("center: closes on that point, not on the centre of the screen", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.2, { + shape: discWithHole(), + center: { x: 12, y: 12 }, + }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 12, 12)).toBe(0); + expect(covered(frame, 52, 52)).toBe(1); + effect.destroy(); + }); +}; diff --git a/packages/melonjs/tests/helpers/pixels.js b/packages/melonjs/tests/helpers/pixels.js index 31aa566198..5a27f80540 100644 --- a/packages/melonjs/tests/helpers/pixels.js +++ b/packages/melonjs/tests/helpers/pixels.js @@ -43,10 +43,12 @@ export async function readPixel(renderer, x, y) { return Array.from(out); } - // WebGPU: async readback off the presented texture - if (typeof renderer.readCanvasPixels === "function") { - const data = await renderer.readCanvasPixels(px, py, 1, 1); - return Array.from(data.data ?? data).slice(0, 4); + // WebGPU: its canvas has no synchronous readback, but still holds the + // frame just submitted until this task ends + if (renderer.type === "WebGPU") { + const frame = await grabFrame(renderer); + const o = (py * frame.width + px) * 4; + return Array.from(frame.data.subarray(o, o + 4)); } // Canvas 2D: top-down rows already @@ -55,6 +57,117 @@ export async function readPixel(renderer, x, y) { return Array.from(ctx.getImageData(px, py, 1, 1).data); } +/** + * Have a WebGPU renderer draw its frames into a texture of its own, in place + * of its canvas's, and return that renderer. + * + * A test reads a frame back from the texture it was drawn into (see + * {@link grabFrame}), and a canvas's texture is only there until the browser + * presents it. Presenting is also what a headless browser on a software + * adapter cannot do: the device is lost the moment a canvas texture is + * asked for. Drawn into a plain texture, the renderer's own work (its + * pipelines, shaders and passes) runs and reads back the same everywhere. + * + * Call it right after `app.init()`, before the first frame. A renderer that + * is not WebGPU is returned as it is. + * @param {object} renderer - any melonJS renderer + * @returns {object} the renderer + */ +export function drawOffscreen(renderer) { + if (renderer.type !== "WebGPU" || renderer.context.__offscreen === true) { + return renderer; + } + const canvas = renderer.getCanvas(); + const context = renderer.context; + const format = + context.getConfiguration?.()?.format ?? + navigator.gpu.getPreferredCanvasFormat(); + let texture; + context.getCurrentTexture = () => { + if ( + texture === undefined || + texture.width !== canvas.width || + texture.height !== canvas.height + ) { + texture?.destroy(); + texture = renderer.device.createTexture({ + label: "test frame", + size: [canvas.width, canvas.height], + format, + usage: + GPUTextureUsage.RENDER_ATTACHMENT | + GPUTextureUsage.COPY_SRC | + GPUTextureUsage.TEXTURE_BINDING, + }); + } + return texture; + }; + context.__offscreen = true; + return renderer; +} + +/** + * The whole frame a renderer has just drawn, as straight-alpha `ImageData`, + * rows top-down, on any backend. + * + * To be called in the task that drew the frame, before anything is awaited: + * a GPU canvas only holds its frame until the browser presents it. On WebGPU + * the frame must have been begun with `renderer.clear()`; it is submitted + * here, and its texture copied into a buffer before this returns its promise + * (see {@link drawOffscreen}, which a test on WebGPU wants first). + * @param {object} renderer - any melonJS renderer + * @returns {Promise} the frame + */ +export async function grabFrame(renderer) { + renderer.flush?.(); + const source = renderer.getCanvas(); + const width = source.width; + const height = source.height; + + if (renderer.type === "WebGPU") { + // the canvas texture of this frame, still current until presented + const device = renderer.device; + const texture = renderer.context.getCurrentTexture(); + const bytesPerRow = Math.ceil((width * 4) / 256) * 256; + const buffer = device.createBuffer({ + size: bytesPerRow * height, + usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, + }); + const encoder = device.createCommandEncoder(); + encoder.copyTextureToBuffer( + { texture }, + { buffer, bytesPerRow }, + { width, height }, + ); + device.queue.submit([encoder.finish()]); + await buffer.mapAsync(GPUMapMode.READ); + const raw = new Uint8Array(buffer.getMappedRange()); + const bgra = texture.format.startsWith("bgra"); + const out = new Uint8ClampedArray(width * height * 4); + for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + const i = y * bytesPerRow + x * 4; + const o = (y * width + x) * 4; + out[o] = raw[i + (bgra ? 2 : 0)]; + out[o + 1] = raw[i + 1]; + out[o + 2] = raw[i + (bgra ? 0 : 2)]; + out[o + 3] = raw[i + 3]; + } + } + buffer.unmap(); + buffer.destroy(); + return new ImageData(out, width, height); + } + + // WebGL and Canvas: copied through a 2D canvas, while the frame is there + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const context = canvas.getContext("2d", { willReadFrequently: true }); + context.drawImage(source, 0, 0); + return context.getImageData(0, 0, width, height); +} + /** * Read the same pixel from several renderers at once, in canvas coordinates. * @param {object[]} renderers - the renderers to sample diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js new file mode 100644 index 0000000000..e45feb4236 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -0,0 +1,614 @@ +/** + * Draw the same tile layers through the CPU renderer (`renderMode` + * "perTile") and through the GPU one ("shader"), with the whole engine in + * between (world, camera, renderer), and compare the two frames. + * + * The tiles are painted pixel by pixel, and every pixel of every tile has a + * colour of its own: a tile that lands one pixel off, is flipped the wrong + * way, or shows in front of the wrong neighbour, changes pixels. With hard + * edges (the default) the two paths are held to the same picture, exactly; + * with soft ones (`alpha`), to a rounding step or two. + */ +import { expect, vi } from "vitest"; +import { Application, boot, TMXTileMap, timer } from "../../src/index.js"; +import { imgList } from "../../src/loader/cache.js"; +import Renderer from "../../src/video/renderer.js"; +import { drawOffscreen, grabFrame } from "./pixels.js"; + +export const FLIP_H = 0x80000000; +export const FLIP_V = 0x40000000; +export const FLIP_AD = 0x20000000; + +let nextName = 0; + +/** + * Whether a pixel of a `w` × `h` tile image belongs to an isometric block + * standing on a `tw` × `th` diamond at the bottom of the image: the diamond, + * the same diamond at the top, and everything between them. A tile as tall + * as its diamond is the diamond alone. + * @param {number} x - pixel column in the tile + * @param {number} y - pixel row in the tile + * @param {number} w - tile image width + * @param {number} h - tile image height + * @param {number} tw - diamond width + * @param {number} th - diamond height + * @returns {boolean} whether the pixel is opaque + */ +export function inBlock(x, y, w, h, tw, th) { + // across: how far from the diamond's vertical axis, in half widths + const dx = Math.abs(x + 0.5 - w / 2) / (tw / 2); + if (dx > 1) { + return false; + } + // the diamond's edge at this column, from its centre line + const edge = (1 - dx) * (th / 2); + const top = th / 2 - edge; + const bottom = h - th / 2 + edge; + return y + 0.5 >= top && y + 0.5 <= bottom; +} + +/** + * Make a tileset image, registered with the loader, and its Tiled JSON. + * @param {object} options + * @param {number} options.tw - tile image width + * @param {number} options.th - tile image height + * @param {number} [options.count=8] - number of tiles + * @param {number} [options.columns=4] - tiles per row of the image + * @param {number} [options.margin=0] - margin round the image + * @param {number} [options.spacing=0] - spacing between tiles + * @param {(x: number, y: number, id: number) => boolean} [options.opaque] + * which pixels of a tile are opaque (all of them by default) + * @param {(x: number, y: number, id: number) => number} [options.alpha] + * how opaque each pixel is, 0 to 255, for soft edges and see-through tiles + * (in place of `opaque`) + * @param {boolean} [options.outerMargin=true] - false for an image that + * stops at its last column and row, with no margin after them, as Tiled + * lets one + * @param {object} [options.extra] - more Tiled tileset properties + * (`tileoffset`, `tiles` with animations...) + * @returns {object} the tileset's Tiled JSON, without its `firstgid` + */ +export function makeTileset(options) { + const { + tw, + th, + count = 8, + columns = 4, + margin = 0, + spacing = 0, + opaque, + alpha, + outerMargin = true, + extra = {}, + } = options; + const rows = Math.ceil(count / columns); + const canvas = document.createElement("canvas"); + const outer = outerMargin ? margin : 0; + canvas.width = margin + outer + columns * tw + (columns - 1) * spacing; + canvas.height = margin + outer + rows * th + (rows - 1) * spacing; + const ctx = canvas.getContext("2d"); + const image = ctx.createImageData(canvas.width, canvas.height); + const seed = nextName * 37; + // the margin and spacing are a colour no tile has: sampled, they show + for (let i = 0; i < image.data.length; i += 4) { + image.data[i] = 255; + image.data[i + 1] = 0; + image.data[i + 2] = 255; + image.data[i + 3] = 255; + } + for (let id = 0; id < count; id++) { + const ox = margin + (id % columns) * (tw + spacing); + const oy = margin + Math.floor(id / columns) * (th + spacing); + for (let y = 0; y < th; y++) { + for (let x = 0; x < tw; x++) { + const o = ((oy + y) * canvas.width + ox + x) * 4; + const a = alpha !== undefined ? alpha(x, y, id) : 255; + if (a <= 0 || (opaque !== undefined && !opaque(x, y, id))) { + image.data[o + 3] = 0; + image.data[o] = image.data[o + 1] = image.data[o + 2] = 0; + continue; + } + // a colour per tile, and per pixel in it: never the + // margin's, never the background's + image.data[o] = 40 + ((id * 53 + seed) % 180); + image.data[o + 1] = 30 + ((x * 7 + id * 11) % 200); + image.data[o + 2] = 30 + ((y * 5 + id * 29) % 200); + image.data[o + 3] = a; + } + } + } + ctx.putImageData(image, 0, 0); + const name = `tile-compare-${nextName++}`; + imgList[name] = canvas; + return { + name, + tilewidth: tw, + tileheight: th, + spacing, + margin, + tilecount: count, + columns, + image: `${name}.png`, + imagewidth: canvas.width, + imageheight: canvas.height, + ...extra, + }; +} + +/** + * Build a map from tilesets and layers. + * @param {object} options + * @param {string} options.orientation - Tiled orientation + * @param {number} options.cols - map width, in tiles + * @param {number} options.rows - map height, in tiles + * @param {number} options.tw - grid tile width + * @param {number} options.th - grid tile height + * @param {object[]} options.tilesets - from {@link makeTileset}; their + * `firstgid` is set here, one after the other + * @param {object[]} options.layers - `{ data }` (gids, row by row) or + * `{ chunks }` for an infinite map, plus any Tiled layer property + * @param {object} [options.extra] - more Tiled map properties + * @returns {TMXTileMap} the map + */ +export function makeMap(options) { + const { + orientation, + cols, + rows, + tw, + th, + tilesets, + layers, + extra = {}, + } = options; + let firstgid = 1; + const sets = tilesets.map((tileset) => { + const set = { ...tileset, firstgid }; + firstgid += tileset.tilecount; + return set; + }); + return new TMXTileMap(`tile-compare-map-${nextName++}`, { + width: cols, + height: rows, + tilewidth: tw, + tileheight: th, + orientation, + renderorder: "right-down", + infinite: false, + version: "1.10", + tiledversion: "1.10.2", + tilesets: sets, + layers: layers.map((layer, i) => { + return { + type: "tilelayer", + name: `layer${i}`, + width: cols, + height: rows, + visible: true, + opacity: 1, + ...layer, + }; + }), + ...extra, + }); +} + +/** + * The picture of a map, painted here with a 2D canvas and no engine: every + * tile of every layer, in the order Tiled draws them (rows from the top, + * each from the left), where Tiled draws them, with nothing left out for + * being out of view. What both of the engine's renderers are held to. + * + * Orthogonal, isometric, staggered and hexagonal maps (the last two take + * their `staggeraxis`, `staggerindex` and `hexsidelength` from the scene's + * `extra`). Tiles are drawn whole, bottom-aligned on their cell and moved by + * their tileset's `tileoffset`; flipped across, down or along the diagonal (the + * last on square tiles only, where a flip is a quarter turn in place). + * A layer is moved by its offset in Tiled (`offsetx`, `offsety`). Layer + * opacity and tint are for the caller to apply to the picture. + * @param {object} scene - what {@link makeMap} was given + * @param {number} viewX - the view's left, in the map's pixels + * @param {number} viewY - the view's top + * @param {number} width - the view's width + * @param {number} height - the view's height + * @returns {ImageData} the picture + */ +export function reference(scene, viewX, viewY, width, height) { + const { orientation, cols, rows, tw, th, tilesets, layers } = scene; + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const ctx = canvas.getContext("2d", { willReadFrequently: true }); + ctx.imageSmoothingEnabled = false; + ctx.fillStyle = "rgb(16, 24, 48)"; + ctx.fillRect(0, 0, width, height); + ctx.translate(-viewX, -viewY); + + let firstgid = 1; + const sets = tilesets.map((tileset) => { + const set = { ...tileset, firstgid, image: imgList[tileset.name] }; + firstgid += tileset.tilecount; + return set; + }); + const drawTile = (raw, left, bottom) => { + const gid = raw & 0x1fffffff; + const set = sets.findLast((candidate) => { + return gid >= candidate.firstgid; + }); + const id = gid - set.firstgid; + const w = set.tilewidth; + const h = set.tileheight; + const sx = set.margin + (id % set.columns) * (w + set.spacing); + const sy = set.margin + Math.floor(id / set.columns) * (h + set.spacing); + const x = left + (set.tileoffset?.x ?? 0); + const y = bottom - h + (set.tileoffset?.y ?? 0); + ctx.save(); + ctx.translate(x + w / 2, y + h / 2); + if ((raw & FLIP_AD) !== 0) { + ctx.rotate(-Math.PI / 2); + ctx.scale(-1, 1); + } + const ad = (raw & FLIP_AD) !== 0; + if ((raw & FLIP_H) !== 0) { + ctx.scale(ad ? 1 : -1, ad ? -1 : 1); + } + if ((raw & FLIP_V) !== 0) { + ctx.scale(ad ? -1 : 1, ad ? 1 : -1); + } + ctx.drawImage(set.image, sx, sy, w, h, -w / 2, -h / 2, w, h); + ctx.restore(); + }; + + for (const layer of layers) { + const ox = layer.offsetx ?? 0; + const oy = layer.offsety ?? 0; + // the layer's cells: a plain grid, or an infinite map's chunks + const cells = new Map(); + const put = (col, row, gid) => { + if (gid !== 0) { + cells.set(`${col},${row}`, gid); + } + }; + if (layer.chunks !== undefined) { + for (const chunk of layer.chunks) { + chunk.data.forEach((gid, i) => { + put( + chunk.x + (i % chunk.width), + chunk.y + Math.floor(i / chunk.width), + gid, + ); + }); + } + } else { + layer.data.forEach((gid, i) => { + put(i % cols, Math.floor(i / cols), gid); + }); + } + const ordered = [...cells.entries()].map(([key, gid]) => { + const [col, row] = key.split(",").map(Number); + return { col, row, gid }; + }); + if (orientation === "isometric") { + // rows of constant col + row, each from the left + ordered.sort((a, b) => { + return a.col + a.row - (b.col + b.row) || a.col - b.col; + }); + for (const { col, row, gid } of ordered) { + drawTile( + gid, + ox + ((col - row - 1) * tw) / 2 + (rows * tw) / 2, + oy + ((col + row + 2) * th) / 2, + ); + } + } else if (orientation === "staggered" || orientation === "hexagonal") { + // Every other row (or column, with `staggeraxis` "x") is + // shifted by half a cell; a hexagonal map's cells are longer + // by `hexsidelength` along the stagger axis. Tiles are drawn + // from the top of the screen down, each line from the left. + const { staggeraxis, staggerindex, hexsidelength = 0 } = scene.extra; + const staggerX = staggeraxis === "x"; + const even = staggerindex === "even" ? 1 : 0; + const side = orientation === "hexagonal" ? hexsidelength : 0; + const sideX = staggerX ? side : 0; + const sideY = staggerX ? 0 : side; + // Tiled's own numbers (libtiled's hexagonal `RenderParams`, as + // of Tiled 1.10), all whole: half of what a side leaves of a + // tile, rounded down; a column and a row; and the cell a tile + // stands on, the tile's size less the pixel dropped + const offsetX = Math.trunc((tw - sideX) / 2); + const offsetY = Math.trunc((th - sideY) / 2); + const columnWidth = offsetX + sideX; + const rowHeight = offsetY + sideY; + const cw = columnWidth + offsetX; + const ch = rowHeight + offsetY; + const place = ({ col, row }) => { + // `& 1` and not `% 2`: an infinite map has negative cells + return staggerX + ? { + x: col * columnWidth, + y: row * (ch + sideY) + ((col & 1) ^ even) * rowHeight, + } + : { + x: col * (cw + sideX) + ((row & 1) ^ even) * columnWidth, + y: row * rowHeight, + }; + }; + const placed = ordered.map((cell) => { + return { ...cell, ...place(cell) }; + }); + placed.sort((a, b) => { + return a.y - b.y || a.x - b.x; + }); + for (const { gid, x, y } of placed) { + drawTile(gid, ox + x, oy + y + ch); + } + } else { + // the map's render order: rows from the top or the bottom, each + // from the left or the right ("right-down" unless said) + const order = scene.extra?.renderorder ?? "right-down"; + const down = order.endsWith("down") ? 1 : -1; + const right = order.startsWith("right") ? 1 : -1; + ordered.sort((a, b) => { + return down * (a.row - b.row) || right * (a.col - b.col); + }); + for (const { col, row, gid } of ordered) { + drawTile(gid, ox + col * tw, oy + (row + 1) * th); + } + } + } + return ctx.getImageData(0, 0, width, height); +} + +/** + * A layer's data, filled by a function of each cell. + * @param {number} cols - columns + * @param {number} rows - rows + * @param {(col: number, row: number) => number} gidOf - the cell's gid, 0 for none + * @returns {number[]} the gids, row by row + */ +export function fill(cols, rows, gidOf) { + const data = []; + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { + data.push(gidOf(col, row)); + } + } + return data; +} + +/** + * The application the comparisons draw with, its loop held still so that + * frames are drawn only when a test asks. `null` when the renderer asked + * for is not available (a WebGPU device, on a machine without one). + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected + * @param {number} [width=320] - canvas width + * @param {number} [height=240] - canvas height + * @returns {Promise} the application + */ +export async function makeApp(renderer, type, width = 320, height = 240) { + // no frame of the engine's own: a test draws each one + vi.spyOn(globalThis, "requestAnimationFrame").mockImplementation(() => { + return 1; + }); + vi.spyOn(globalThis, "cancelAnimationFrame").mockImplementation(() => {}); + boot(); + let app; + try { + app = new Application(width, height, { + parent: "screen", + renderer, + consoleHeader: false, + }); + await app.init(); + } catch { + return null; + } + if (!app.renderer.type.startsWith(type)) { + app.destroy(); + return null; + } + drawOffscreen(app.renderer); + // not black: a tile that is not drawn shows + app.renderer.backgroundColor.setColor(16, 24, 48); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + return app; +} + +/** + * Put a map's layers in the world, in place of whatever was there. + * @param {Application} app - the application + * @param {TMXTileMap} map - the map + * @param {string} [renderMode] - a render mode to ask of every layer before + * it is activated ("prerender" needs asking then: it bakes at activation) + * @param {boolean} [centered=false] - as `level.load` adds a map: one + * smaller than the view is centred in it, by moving the world (`world.pos`) + * @returns {object[]} the map's tile layers + */ +export function show(app, map, renderMode, centered = false) { + app.world.reset(); + // where a map centred before this one left it + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + if (renderMode !== undefined) { + for (const layer of map.getLayers()) { + layer.renderMode = renderMode; + } + } + map.addTo(app.world, true, centered); + // the layers in their order now: the world sorts its children later, on + // a timer no frame of these tests waits for + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + return map.getLayers(); +} + +let clock = 0; + +/** + * Draw one frame with every layer in the given mode, and return it with how + * many times the CPU tile renderer was reached while drawing it. + * @param {Application} app - the application + * @param {object[]} layers - the layers + * @param {string} mode - "perTile", "shader" or "prerender" + * @param {number} [dt=0] - game time to pass before the frame, in ms + * @returns {Promise<{frame: ImageData, cpuDraws: number}>} the frame + */ +export async function drawIn(app, layers, mode, dt = 0) { + for (const layer of layers) { + layer.renderMode = mode; + } + // time passes (animated tiles advance once per tick of the engine's + // clock), then the world is updated: what is in view, what has changed + if (dt > 0) { + clock = Math.max(clock, timer.getTime()) + dt; + timer.update(clock); + } + app.world.update(dt); + app.viewport.update(dt); + const cpu = vi.spyOn(Renderer.prototype, "drawTileLayer"); + app.repaint(); + app.draw(); + const cpuDraws = cpu.mock.calls.length; + cpu.mockRestore(); + const frame = await grabFrame(app.renderer); + return { frame, cpuDraws }; +} + +/** + * Compare two frames. + * @param {ImageData} a - one frame + * @param {ImageData} b - the other + * @param {number} [tolerance=0] - largest difference of a channel that is + * not counted + * @returns {{different: number, worst: number, first: string}} how many + * pixels differ, by how much at most, and the first one + */ +export function diff(a, b, tolerance = 0) { + let different = 0; + let worst = 0; + let first = ""; + for (let i = 0; i < a.data.length; i += 4) { + const d = Math.max( + Math.abs(a.data[i] - b.data[i]), + Math.abs(a.data[i + 1] - b.data[i + 1]), + Math.abs(a.data[i + 2] - b.data[i + 2]), + ); + if (d > tolerance) { + different++; + if (first === "") { + const p = i / 4; + first = `(${p % a.width}, ${Math.floor(p / a.width)}): [${a.data.slice(i, i + 3)}] against [${b.data.slice(i, i + 3)}]`; + } + } + worst = Math.max(worst, d); + } + return { different, worst, first }; +} + +/** + * How many pixels of a frame are not the background: how much was drawn. + * @param {ImageData} frame - the frame + * @returns {number} the count + */ +export function drawn(frame) { + let count = 0; + for (let i = 0; i < frame.data.length; i += 4) { + if ( + frame.data[i] !== 16 || + frame.data[i + 1] !== 24 || + frame.data[i + 2] !== 48 + ) { + count++; + } + } + return count; +} + +/** views over a map: inside, over each edge, and scrolled part way */ +const DEFAULT_VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + [-150, 30], + [100, -200], +]; + +/** + * Draw a scene both ways from each view, and expect the reference + * picture from both, the GPU path drawing alone. + * @param {Application} app - the application, from {@link makeApp} + * @param {object} scene - what {@link makeMap} takes + * @param {object} [options] + * @param {number[][]} [options.views] - where to look from + * @param {object[]} [options.layers] - the scene's layers, when + * they are in the world already (and were changed there). Without + * them the scene is put in the world here, and every layer of it is + * expected to take the GPU path of its own accord + * @param {boolean} [options.oracle=true] - false where the reference + * does not reach (a zoomed camera): the two paths are then compared + * with each other alone + * @param {(picture: ImageData) => ImageData} [options.expected] + * what the reference becomes (a faded or tinted layer) + * @param {number} [options.tolerance=0] - the largest difference of a + * channel that is not counted + * @param {number} [options.share=0] - the largest share of the pixels + * drawn that the two paths may differ by + * @param {number[]} [options.origin] - where the map's origin is drawn, + * when the world or a container the layers are in was moved + * @returns {Promise} how many pixels were drawn, in all + */ +export async function comparePaths(app, scene, options = {}) { + const { + views = DEFAULT_VIEWS, + oracle = true, + expected, + tolerance = 0, + share = 0, + origin = [0, 0], + } = options; + const { width, height } = app.viewport; + let layers = options.layers; + if (layers === undefined) { + layers = show(app, makeMap(scene)); + for (const layer of layers) { + expect(layer.renderMode, layer.name).toBe("shader"); + } + } + let total = 0; + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + // a layer out of view is not drawn at all; one in view is + // drawn tile by tile by the CPU path, and never by the GPU one + expect(cpu.cpuDraws).toBeLessThanOrEqual(layers.length); + expect(gpu.cpuDraws, "the GPU path reaches no CPU renderer").toBe(0); + const at = `view (${x}, ${y})`; + const count = drawn(gpu.frame); + const allowed = Math.floor(count * share); + if (oracle) { + let ref = reference(scene, x - origin[0], y - origin[1], width, height); + if (expected !== undefined) { + ref = expected(ref); + } + const c = diff(ref, cpu.frame, tolerance); + expect(c.different, `${at}, CPU path: ${c.first}`).toBe(0); + // and where something is to be seen, the layer was asked for it + if (drawn(ref) > 0) { + expect(cpu.cpuDraws, `${at}, CPU draws`).toBeGreaterThan(0); + } + const g = diff(ref, gpu.frame, tolerance); + expect(g.different, `${at}, GPU path: ${g.first}`).toBe(0); + } + const d = diff(cpu.frame, gpu.frame, tolerance); + expect( + d.different, + `${at}, CPU against GPU: ${d.first}`, + ).toBeLessThanOrEqual(allowed); + total += count; + } + return total; +} diff --git a/packages/melonjs/tests/helpers/tile-layer-edge-cases.js b/packages/melonjs/tests/helpers/tile-layer-edge-cases.js new file mode 100644 index 0000000000..15bdf446e6 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-edge-cases.js @@ -0,0 +1,715 @@ +/** + * Tile layers at their edges: the cases a set of adversarial probes broke, + * each held to the picture painted with no engine (`tile-layer-compare.js`). + * A layer the GPU path cannot draw right is left to the CPU renderer, with a + * word in the console, and is still drawn as Tiled draws it. + * + * One spec file per GPU backend calls this. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { Container } from "../../src/index.js"; +import { + comparePaths, + diff, + drawIn, + drawn, + fill, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +const W = 320; +const H = 240; + +const all = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return 1 + ((c + r * 3) % 8); + }); +}; + +/** + * Declare the cases for one GPU backend. + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected ("WebGL", "WebGPU") + */ +export function edgeCases(renderer, type) { + describe(`tile layers at their edges (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, W, H); + }); + afterAll(() => { + app?.world.pos.set(0, 0, 0); + app?.destroy(); + vi.restoreAllMocks(); + }); + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + + /** + * Put a scene in the world, and say how its first layer is drawn and + * what was warned. + */ + const shown = (scene) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layers = show(app, makeMap(scene)); + return { + layers, + mode: layers[0].renderMode, + said: warn.mock.calls.flat().join(" "), + }; + } finally { + warn.mockRestore(); + } + }; + + /** + * A layer left to the CPU renderer for `reason`: drawn as the + * reference, as it stands and when asked for the GPU path by hand. + */ + const fallsBack = async (scene, reason, views = [[-40, -60]]) => { + const { layers, mode, said } = shown(scene); + expect(mode).toBe("perTile"); + expect(said).toMatch(reason); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const ref = reference(scene, x, y, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + const cpu = await drawIn(app, layers, "perTile"); + const c = diff(ref, cpu.frame); + expect(c.different, `tile by tile: ${c.first}`).toBe(0); + // by hand: still tile by tile, and the same picture + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(layers.length); + const f = diff(ref, forced.frame); + expect(f.different, `asked for by hand: ${f.first}`).toBe(0); + } + }; + + describe("what is in view", () => { + it("isometric, tile by tile: a tile narrower than half a cell, at the right edge of the view", async (ctx) => { + need(ctx); + // the view's corners are rounded to whole cells, half a cell + // either way: a tile 8 pixels wide does not make up for it + const scene = { + orientation: "isometric", + cols: 1, + rows: 1, + tw: 64, + th: 32, + tilesets: [makeTileset({ tw: 8, th: 8 })], + layers: [{ data: [1] }], + }; + const layers = show(app, makeMap(scene)); + // the one tile is drawn at x 0 to 7: these views end 1 to 8 + // pixels right of its left edge + for (let right = 1; right <= 8; right++) { + const x = right - W; + app.viewport.moveTo(x, -120); + const ref = reference(scene, x, -120, W, H); + expect(drawn(ref), "the tile is in view").toBe(right * 8); + for (const mode of ["shader", "perTile"]) { + const { frame } = await drawIn(app, layers, mode); + const d = diff(ref, frame); + expect(d.different, `${mode}, ${right} px in view`).toBe(0); + } + } + }); + + for (const [orientation, tw, th, extra] of [ + ["orthogonal", 32, 32, {}], + ["isometric", 64, 32, {}], + ["staggered", 64, 32, { staggeraxis: "y", staggerindex: "odd" }], + [ + "hexagonal", + 28, + 24, + { staggeraxis: "x", staggerindex: "even", hexsidelength: 12 }, + ], + ]) { + it(`${orientation}: a sliver of the map, a pixel or a few, at each edge of the view`, async (ctx) => { + need(ctx); + // tiles larger than a cell, moved by a tileoffset where + // the GPU takes one: how far the map reaches is measured + // on the reference itself + const scene = { + orientation, + cols: 5, + rows: 5, + tw, + th, + tilesets: [ + makeTileset({ + tw: tw + 10, + th: th * 2, + // (an orthogonal tileset with an offset is left + // to the CPU renderer) + extra: + orientation === "orthogonal" + ? {} + : { tileoffset: { x: -7, y: 5 } }, + }), + ], + layers: [{ data: all(5, 5) }], + extra, + }; + const wide = reference(scene, -400, -400, 1200, 1000); + let left = Infinity; + let top = Infinity; + let right = -Infinity; + let bottom = -Infinity; + for (let y = 0; y < wide.height; y++) { + for (let x = 0; x < wide.width; x++) { + const i = (y * wide.width + x) * 4; + if ( + wide.data[i] !== 16 || + wide.data[i + 1] !== 24 || + wide.data[i + 2] !== 48 + ) { + left = Math.min(left, x - 400); + right = Math.max(right, x - 400 + 1); + top = Math.min(top, y - 400); + bottom = Math.max(bottom, y - 400 + 1); + } + } + } + expect(right - left).toBeGreaterThan(100); + const middleX = Math.round((left + right - W) / 2); + const middleY = Math.round((top + bottom - H) / 2); + const views = []; + for (const k of [1, 2, 5]) { + // the map's left edge at the view's right, and so on + views.push([left + k - W, middleY]); + views.push([right - k, middleY]); + views.push([middleX, top + k - H]); + views.push([middleX, bottom - k]); + } + const layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + for (const view of views) { + expect( + await comparePaths(app, scene, { views: [view], layers }), + `view (${view})`, + ).toBeGreaterThan(0); + } + // and a pixel out of sight: nothing, either way + const out = [ + [left - W, middleY], + [right, middleY], + [middleX, top - H], + [middleX, bottom], + ]; + expect(await comparePaths(app, scene, { views: out, layers })).toBe( + 0, + ); + }); + } + + it("a layer in a container with a size: the tops of tall tiles, the grid out of view", async (ctx) => { + need(ctx); + // a level loaded into a container gives it the map's size: + // the camera has the container in view by its bounds before + // it asks the layer, whose tiles stand 64 pixels above + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 96 })], + layers: [{ data: all(4, 3) }], + }; + // the view ends 10 pixels above the grid's first row + const view = [-40, -H - 10]; + const ref = reference(scene, ...view, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + for (const boxed of [false, true]) { + app.world.reset(); + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + const map = makeMap(scene); + const box = boxed ? new Container() : app.world; + if (boxed) { + app.world.addChild(box); + } + map.addTo(box, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + app.viewport.moveTo(...view); + for (const mode of ["perTile", "shader"]) { + const { frame } = await drawIn(app, map.getLayers(), mode); + const d = diff(ref, frame); + expect( + d.different, + `${boxed ? "in a container" : "in the world"}, ${mode}: ${d.first}`, + ).toBe(0); + } + } + }); + + /** a scene put in a container of its own, as a level loaded into one */ + const boxed = (scene) => { + app.world.reset(); + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + const map = makeMap(scene); + const box = new Container(); + app.world.addChild(box); + map.addTo(box, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + return map.getLayers(); + }; + + it("a layer offset in Tiled, in a container with a size: drawn where it is, the map's rectangle out of view", async (ctx) => { + need(ctx); + // the container has the map's size: 160 wide, and the layer + // is 200 to its right + const scene = { + orientation: "orthogonal", + cols: 5, + rows: 4, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(5, 4), offsetx: 200, offsety: 0 }], + }; + const view = [170, -40]; + const ref = reference(scene, ...view, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + const layers = boxed(scene); + app.viewport.moveTo(...view); + for (const mode of ["perTile", "shader"]) { + const { frame } = await drawIn(app, layers, mode); + const d = diff(ref, frame); + expect(d.different, `${mode}: ${d.first}`).toBe(0); + } + }); + + it("an infinite map in a container: the tiles left of and above its origin", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { x, y, width: 4, height: 4, data: all(4, 4) }; + }; + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 4, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [ + { + startx: -4, + starty: -8, + width: 8, + height: 12, + data: undefined, + chunks: [chunk(-3, -5), chunk(0, 0)], + }, + ], + extra: { infinite: true }, + }; + // left of and above the map's own rectangle + const view = [-W - 4, -H - 4]; + const ref = reference(scene, ...view, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + const layers = boxed(scene); + app.viewport.moveTo(...view); + for (const mode of ["perTile", "shader"]) { + const { frame } = await drawIn(app, layers, mode); + const d = diff(ref, frame); + expect(d.different, `${mode}: ${d.first}`).toBe(0); + } + }); + + for (const mode of ["perTile", "shader", "prerender"]) { + it(`a zoomed camera with an offset, over a moved world, drawn "${mode}"`, async (ctx) => { + need(ctx); + // the camera at `pos + offset` with no offset shows the + // same thing, by definition: zoomed, it is not the + // default camera, and draws the world unmoved + const scene = { + orientation: "orthogonal", + cols: 12, + rows: 12, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(12, 12) }], + }; + app.world.reset(); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + // the world moved first, as a centred map leaves it + app.world.pos.set(100, 60, app.world.pos.z); + const map = makeMap(scene); + const layers = map.getLayers(); + if (mode === "prerender") { + layers[0].renderMode = mode; + } + map.addTo(app.world, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + try { + for (const zoom of [1, 2]) { + app.viewport.zoom = zoom; + app.viewport.offset.set(0, 0); + app.viewport.moveTo(60 + 26, 40 - 18); + const moved = await drawIn(app, layers, mode); + expect(drawn(moved.frame)).toBeGreaterThan(0); + app.viewport.offset.set(26, -18); + app.viewport.moveTo(60, 40); + const shaken = await drawIn(app, layers, mode); + const d = diff(moved.frame, shaken.frame); + expect(d.different, `zoom ${zoom}: ${d.first}`).toBe(0); + } + } finally { + app.viewport.zoom = 1; + app.viewport.offset.set(0, 0); + app.world.pos.set(0, 0, app.world.pos.z); + } + }); + } + }); + + describe("grids of odd sizes, laid out as Tiled lays them out", () => { + it("a staggered map of odd tile size: on cells a pixel smaller, every way it is drawn", async (ctx) => { + need(ctx); + // 63 x 31 tiles stand on 62 x 30 cells: one pixel above and + // one to the right of each + const scene = { + orientation: "staggered", + cols: 4, + rows: 4, + tw: 63, + th: 31, + tilesets: [makeTileset({ tw: 63, th: 31 })], + layers: [{ data: all(4, 4) }], + extra: { staggeraxis: "y", staggerindex: "odd" }, + }; + const views = [ + [-20, -20], + [40, 10], + ]; + expect(await comparePaths(app, scene, { views })).toBeGreaterThan(0); + const baked = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, baked, "prerender"); + const d = diff(reference(scene, x, y, W, H), frame); + expect(d.different, `baked: ${d.first}`).toBe(0); + } + }); + + for (const [axis, tw, th, side] of [ + ["x", 28, 24, 11], + // on this axis the cell is a pixel shorter than the tile + ["y", 28, 24, 11], + // an odd height: 7 a side, rows 18 apart + ["y", 28, 25, 11], + ["x", 30, 24, 7], + // longer than the tile: the half left of a cell is negative + ["x", 28, 24, 33], + ]) { + it(`a hexagon side of ${side} on ${tw} x ${th} cells, axis ${axis}: columns and rows on whole pixels`, async (ctx) => { + need(ctx); + // Tiled halves what a side leaves of a cell in whole + // numbers: 28 wide with a side of 11 is columns 19 + // pixels apart, not 19.5 + const scene = { + orientation: "hexagonal", + cols: 6, + rows: 5, + tw, + th, + tilesets: [makeTileset({ tw, th })], + layers: [{ data: all(6, 5) }], + extra: { + staggeraxis: axis, + staggerindex: "odd", + hexsidelength: side, + }, + }; + const views = [ + [-20, -20], + [35, 12], + ]; + expect(await comparePaths(app, scene, { views })).toBeGreaterThan(0); + // and baked: the canvas holds every tile, to its last pixel + const baked = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, baked, "prerender"); + const d = diff(reference(scene, x, y, W, H), frame); + expect(d.different, `baked: ${d.first}`).toBe(0); + } + }); + } + }); + + describe("orthogonal layers whose tiles overlap", () => { + const big = (options = {}) => { + return makeTileset({ tw: 48, th: 48, ...options }); + }; + + it("tiles of two tilesets, larger than the grid, in one layer", async (ctx) => { + need(ctx); + // a later cell lies over an earlier one, whichever tileset + // its tile is from: a pass per tileset cannot keep that + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [big(), big()], + layers: [ + { + // a checkerboard of the two tilesets + data: fill(4, 3, (c, r) => { + return 1 + ((c + r) % 2) * 8 + ((c + r * 3) % 8); + }), + }, + ], + }; + await fallsBack(scene, /more than one tileset/); + }); + + it("tiles of two tilesets, the size of the grid: nothing overlaps, and the GPU draws them", async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ tw: 32, th: 32 }), + ], + layers: [ + { + data: fill(6, 5, (c, r) => { + return 1 + ((c + r) % 2) * 8 + ((c + r * 3) % 8); + }), + }, + ], + }; + expect(await comparePaths(app, scene)).toBeGreaterThan(0); + }); + + it("a large tileset in the map that the layer does not use", async (ctx) => { + need(ctx); + // the layer's own tiles are the size of the grid + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ tw: 32, th: 32 }), + big(), + ], + layers: [ + { + data: fill(6, 5, (c, r) => { + return 1 + ((c + r) % 2) * 8 + ((c + r * 3) % 8); + }), + }, + ], + }; + const { layers, mode } = shown(scene); + expect(mode).toBe("shader"); + expect(await comparePaths(app, scene, { layers })).toBeGreaterThan(0); + // until a tile of the large one is set: three tilesets in + // use, one of them overlapping + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + scene.layers[0].data[2 * 6 + 2] = 19; + layers[0].setTile(layers[0].getTileById(19, 2, 2), 2, 2); + expect(layers[0].renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /more than one tileset/, + ); + } finally { + warn.mockRestore(); + } + app.viewport.moveTo(0, 0); + const { frame } = await drawIn(app, layers, "perTile"); + const d = diff(reference(scene, 0, 0, W, H), frame); + expect(d.different, d.first).toBe(0); + }); + + for (const order of ["right-up", "left-down", "left-up"]) { + it(`render order "${order}", tiles larger than the grid`, async (ctx) => { + need(ctx); + // the map walks its rows and columns the way it says: + // with "left-up" the first cell's tile is in front + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [big()], + layers: [{ data: all(4, 3) }], + extra: { renderorder: order }, + }; + await fallsBack(scene, /render order/); + }); + + it(`render order "${order}", tiles the size of the grid: on the GPU`, async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(6, 5) }], + extra: { renderorder: order }, + }; + expect(await comparePaths(app, scene)).toBeGreaterThan(0); + }); + } + }); + + describe("layers past what the GPU holds, and tiles the image does not", () => { + for (const orientation of ["orthogonal", "isometric"]) { + it(`${orientation}: a layer with more columns than the largest texture has pixels`, async (ctx) => { + need(ctx); + // the GPU path keeps a texel per cell + const cols = app.renderer.maxTextureSize + 1; + expect(Number.isFinite(cols)).toBe(true); + const iso = orientation === "isometric"; + const scene = { + orientation, + cols, + rows: 1, + tw: iso ? 64 : 32, + th: 32, + tilesets: [makeTileset({ tw: iso ? 64 : 32, th: 32 })], + layers: [ + { + data: fill(cols, 1, (c) => { + return 1 + (c % 8); + }), + }, + ], + }; + await fallsBack(scene, /largest texture/, [[0, iso ? -100 : 0]]); + }); + } + + it("a tile the tileset's image does not hold is not drawn, either way", async (ctx) => { + need(ctx); + // an image cropped since the map was saved: 4 tiles of the 8 + // the tileset says it has + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32, count: 4, extra: { tilecount: 8 } }), + ], + layers: [{ data: all(6, 5) }], + }; + const { layers } = shown(scene); + app.viewport.moveTo(0, 0); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + // the tiles the image holds, and none of the others + const held = { + ...scene, + layers: [ + { + data: scene.layers[0].data.map((gid) => { + return gid <= 4 ? gid : 0; + }), + }, + ], + }; + const ref = reference(held, 0, 0, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + expect(diff(ref, cpu.frame).different, "tile by tile").toBe(0); + expect(diff(ref, gpu.frame).different, "on the GPU").toBe(0); + }); + + it("a tile the image does not hold, set on a baked layer: left undrawn", async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32, count: 4, extra: { tilecount: 8 } }), + ], + layers: [ + { + data: fill(6, 5, (c, r) => { + return 1 + ((c + r) % 4); + }), + }, + ], + }; + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layers = show(app, makeMap(scene), "prerender"); + const layer = layers[0]; + // one the image holds, and one it does not + scene.layers[0].data[1 * 6 + 1] = 3; + layer.setTile(layer.getTileById(3, 1, 1), 1, 1); + scene.layers[0].data[2 * 6 + 2] = 0; + expect(() => { + layer.setTile(layer.getTileById(7, 2, 2), 2, 2); + }).not.toThrow(); + app.viewport.moveTo(0, 0); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, 0, 0, W, H), frame); + expect(d.different, d.first).toBe(0); + } finally { + warn.mockRestore(); + } + }); + + it("a layer taken out of the world and put back is drawn again", async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(6, 5) }], + }; + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + app.world.removeChildNow(layer, true); + expect(() => { + app.world.addChild(layer); + }).not.toThrow(); + app.world.sortNow(true); + expect(layer.renderMode).toBe("shader"); + expect(await comparePaths(app, scene, { layers })).toBeGreaterThan(0); + }); + }); + }); +} diff --git a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js new file mode 100644 index 0000000000..1764611e2e --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js @@ -0,0 +1,1632 @@ +/** + * Tile layers on the GPU ("shader" render mode), isometric ones above all + * (#1897), held to the picture Tiled draws. + * + * Every scenario draws the same layers through the CPU renderer ("perTile") + * and through the GPU one, with the whole engine in between, and compares + * both frames pixel by pixel with a reference painted here with no engine + * at all (see `tile-layer-compare.js`). Where the reference does not reach + * (a faded or tinted layer, a zoomed camera, animated tiles), the two paths + * are compared with each other. + * + * The scenarios are the same on every GPU backend, so each one's spec file + * calls this with its renderer: an application with a GPU device takes the + * place of the one before it, and a spec file has its own page. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { grabFrame } from "./pixels.js"; +import { + comparePaths, + diff, + drawIn, + drawn, + FLIP_AD, + FLIP_H, + FLIP_V, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +const TW = 64; +const TH = 32; + +/** a tileset of isometric blocks, `tw` × `th`, on the 64 × 32 grid */ +const blocks = (tw = TW, th = TH, options = {}) => { + return makeTileset({ + tw, + th, + opaque: (x, y) => { + return inBlock(x, y, tw, th, Math.min(tw, TW), TH); + }, + ...options, + }); +}; + +/** + * a tileset of blocks with soft edges, as drawn art has: a pixel is as + * opaque as the share of its 3 × 3 neighbours that are in the block. Every + * fourth tile is see-through all over, as glass or water is + */ +const softBlocks = (tw, th, options = {}) => { + const dw = Math.min(tw, TW); + return makeTileset({ + tw, + th, + alpha: (x, y, id) => { + let inside = 0; + for (let dy = -1; dy <= 1; dy++) { + for (let dx = -1; dx <= 1; dx++) { + inside += inBlock(x + dx, y + dy, tw, th, dw, TH) ? 1 : 0; + } + } + return Math.round((inside / 9) * (id % 4 === 3 ? 120 : 255)); + }, + ...options, + }); +}; + +/** a map with most cells filled, from one tileset, and a few holes */ +const scattered = (cols, rows, count = 8, firstgid = 1) => { + return fill(cols, rows, (c, r) => { + return (c * 3 + r) % 7 === 0 ? 0 : firstgid + ((c + r * 2) % count); + }); +}; + +/** an isometric scene, on the 64 × 32 grid */ +const iso = (options) => { + return { orientation: "isometric", tw: TW, th: TH, ...options }; +}; + +const WIDTH = 320; +const HEIGHT = 240; + +/** views over a map: inside, over each edge, and scrolled part way */ +const VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + [-150, 30], + [100, -200], +]; + +/** the frames' background, as `makeApp` sets it */ +const BACKGROUND = [16, 24, 48]; + +/** + * What a picture becomes when its layer is faded or tinted as a whole: each + * drawn pixel multiplied by the tint, then blended with the background. + * @param {ImageData} picture - the reference picture + * @param {number} opacity - the layer's opacity + * @param {number[]} [tint] - the layer's tint, 0 to 255 a channel + * @returns {ImageData} the picture expected + */ +const layered = (picture, opacity, tint = [255, 255, 255]) => { + const out = new ImageData(picture.width, picture.height); + for (let i = 0; i < picture.data.length; i += 4) { + const isBackground = + picture.data[i] === BACKGROUND[0] && + picture.data[i + 1] === BACKGROUND[1] && + picture.data[i + 2] === BACKGROUND[2]; + for (let c = 0; c < 3; c++) { + out.data[i + c] = isBackground + ? BACKGROUND[c] + : Math.round( + ((picture.data[i + c] * tint[c]) / 255) * opacity + + BACKGROUND[c] * (1 - opacity), + ); + } + out.data[i + 3] = 255; + } + return out; +}; + +const ALL_FLIPS = [ + 0, + FLIP_H, + FLIP_V, + FLIP_H + FLIP_V, + FLIP_AD, + FLIP_H + FLIP_AD, + FLIP_V + FLIP_AD, + FLIP_H + FLIP_V + FLIP_AD, +]; + +/** + * Declare the scenarios for one GPU backend. + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected ("WebGL", "WebGPU") + */ +export function isometricScenarios(renderer, type) { + describe(`tile layers on the GPU, against the CPU renderer and a reference (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + + const same = (scene, options) => { + return comparePaths(app, scene, options); + }; + + /** how a layer of the scene ends up drawn, and what was warned */ + const modeOf = (scene) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layer = show(app, makeMap(scene))[0]; + return [layer.renderMode, warn.mock.calls.flat().join(" ")]; + } finally { + warn.mockRestore(); + } + }; + + describe("isometric: tiles of every size", () => { + for (const [label, tw, th] of [ + ["as tall as the grid (flat diamonds)", 64, 32], + ["twice as tall", 64, 64], + ["four times as tall, as the Isometric Dungeon's", 64, 128], + ["eight times as tall, the limit", 64, 256], + ["a height that is no multiple of the grid", 64, 50], + ["shorter than the grid", 64, 20], + ["narrower than the grid", 40, 64], + ["twice as wide", 128, 32], + ["four times as wide, the limit", 256, 64], + ["wider and taller, no multiple of the grid", 90, 70], + ]) { + it(label, async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(tw, th)], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + for (const [tw, th] of [ + [64, 96], + [128, 128], + [100, 30], + ]) { + it(`${tw} × ${th} tiles that fill their whole image, so every overlap shows`, async (ctx) => { + need(ctx); + // no transparent pixel: which tile is in front decides + // every pixel where two images overlap + const scene = iso({ + cols: 7, + rows: 9, + tilesets: [makeTileset({ tw, th })], + layers: [{ data: scattered(7, 9) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + it("on a grid of another size, and an odd one", async (ctx) => { + need(ctx); + for (const [tw, th] of [ + [32, 16], + [100, 50], + [30, 14], + [48, 48], + ]) { + const scene = { + orientation: "isometric", + cols: 9, + rows: 7, + tw, + th, + tilesets: [ + makeTileset({ + tw, + th: th * 2, + opaque: (x, y) => { + return inBlock(x, y, tw, th * 2, tw, th); + }, + }), + ], + layers: [{ data: scattered(9, 7) }], + }; + expect(await same(scene), `${tw} × ${th}`).toBeGreaterThan(0); + } + }); + }); + + describe("isometric: maps of every shape", () => { + for (const [cols, rows] of [ + [1, 1], + [1, 7], + [7, 1], + [3, 11], + [12, 4], + [20, 20], + ]) { + it(`${cols} × ${rows}`, async (ctx) => { + need(ctx); + const scene = iso({ + cols, + rows, + tilesets: [blocks(64, 64)], + layers: [ + { + data: fill(cols, rows, (c, r) => { + return 1 + ((c + r * 3) % 8); + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + it("a large map, of tiles like the Isometric Dungeon's", async (ctx) => { + need(ctx); + const scene = { + orientation: "isometric", + cols: 64, + rows: 64, + tw: 128, + th: 64, + tilesets: [ + makeTileset({ + tw: 128, + th: 256, + count: 16, + columns: 8, + opaque: (x, y) => { + return inBlock(x, y, 128, 256, 128, 64); + }, + }), + ], + layers: [{ data: scattered(64, 64, 16) }], + }; + const views = [ + [4096 - 160, 0], + [4096 - 160, 2000], + [100, 2040], + [7900, 2040], + [4096, 4000], + [3000, 1500], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + }); + + it("an empty layer draws nothing, either way", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks()], + layers: [ + { + data: fill(6, 6, () => { + return 0; + }), + }, + ], + }); + expect(await same(scene, { views: [[0, 0]] })).toBe(0); + }); + + it("a single tile, and tiles in the corners alone", async (ctx) => { + need(ctx); + const corners = iso({ + cols: 9, + rows: 5, + tilesets: [blocks(64, 128)], + layers: [ + { + data: fill(9, 5, (c, r) => { + return (c === 0 || c === 8) && (r === 0 || r === 4) ? 3 : 0; + }), + }, + ], + }); + expect(await same(corners)).toBeGreaterThan(0); + const one = iso({ + cols: 9, + rows: 5, + tilesets: [blocks(64, 128)], + layers: [ + { + data: fill(9, 5, (c, r) => { + return c === 4 && r === 2 ? 6 : 0; + }), + }, + ], + }); + expect(await same(one)).toBeGreaterThan(0); + }); + }); + + describe("isometric: what a tile can be", () => { + it("flipped, every way", async (ctx) => { + need(ctx); + // square tiles: a diagonal flip turns the image a quarter + const tileset = makeTileset({ + tw: 64, + th: 64, + opaque: (x, y) => { + return x + y > 20; + }, + }); + const scene = iso({ + cols: 8, + rows: 8, + tilesets: [tileset], + layers: [ + { + data: fill(8, 8, (c, r) => { + return 1 + ((c + r) % 8) + ALL_FLIPS[(c * 3 + r) % 8]; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("flipped across and down, taller than wide", async (ctx) => { + need(ctx); + const flips = [0, FLIP_H, FLIP_V, FLIP_H + FLIP_V]; + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [ + { + data: fill(8, 6, (c, r) => { + return 1 + ((c + r) % 8) + flips[(c + r * 3) % 4]; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("animated: each frame, as time passes", async (ctx) => { + need(ctx); + const tileset = blocks(64, 64, { + extra: { + tiles: [ + { + id: 0, + animation: [ + { tileid: 0, duration: 100 }, + { tileid: 5, duration: 100 }, + { tileid: 6, duration: 100 }, + ], + }, + { + id: 3, + animation: [ + { tileid: 3, duration: 150 }, + { tileid: 7, duration: 150 }, + ], + }, + ], + }, + }); + const layers = show( + app, + makeMap( + iso({ + cols: 8, + rows: 6, + tilesets: [tileset], + layers: [ + { + data: fill(8, 6, (c, r) => { + return 1 + ((c + r) % 4); + }), + }, + ], + }), + ), + ); + expect(layers[0].isAnimated).toBe(true); + expect(layers[0].renderMode).toBe("shader"); + app.viewport.moveTo(0, 0); + const frames = []; + for (let step = 0; step < 6; step++) { + // 60 ms a step: the two animations change at their own times + const cpu = await drawIn(app, layers, "perTile", 60); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const d = diff(cpu.frame, gpu.frame); + expect(d.different, `step ${step}: ${d.first}`).toBe(0); + frames.push(gpu.frame); + } + // and the picture did change along the way + expect(diff(frames[0], frames[2]).different).toBeGreaterThan(0); + expect(diff(frames[2], frames[5]).different).toBeGreaterThan(0); + }); + + it("from a tileset with a margin and spacing", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 64, { margin: 3, spacing: 5 })], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("from a tileset whose image has no margin after its last tiles", async (ctx) => { + need(ctx); + // as Tiled lets one be: the tiles are counted from the first + // margin, not as if there were two + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [ + blocks(64, 64, { margin: 7, spacing: 2, outerMargin: false }), + ], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + for (const [x, y] of [ + [0, 16], + [-12, 0], + [7, -23], + // further than a tile is large: the tile shows in cells that + // are not its own, and from views its cell is not in + [0, -80], + [150, 150], + [-200, 90], + ]) { + it(`moved by a tileset tileoffset of (${x}, ${y})`, async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 64, { extra: { tileoffset: { x, y } } })], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + for (const [tw, th] of [ + [64, 32], + [64, 64], + [64, 128], + [128, 96], + ]) { + it(`with soft edges, and see-through, ${tw} × ${th}: each shows the tiles behind it`, async (ctx) => { + need(ctx); + // a texel that is not opaque is blended over the tiles + // behind it, as many as there are: one over the other + // in 8 bits a step tile by tile, in one go on the GPU, + // a rounding step or two apart + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [softBlocks(tw, th)], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene, { tolerance: 3 })).toBeGreaterThan(0); + }); + } + + it("from the map's second tileset", async (ctx) => { + need(ctx); + // the first tileset is not used by this layer at all, and is + // of another size: nothing of it may count + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [{ data: scattered(8, 6, 8, 9) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("from a tileset smaller than another of the map's", async (ctx) => { + need(ctx); + // the other way round: the unused tileset is the large one + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(128, 256), blocks(64, 48)], + layers: [{ data: scattered(8, 6, 8, 9) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + }); + + describe("isometric: what a layer can be", () => { + it("half transparent, its tiles side by side", async (ctx) => { + need(ctx); + // flat diamonds do not overlap: each pixel is blended once, + // a rounding step apart at most + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 32)], + layers: [{ data: scattered(8, 6), opacity: 0.5 }], + }); + const expected = (picture) => { + return layered(picture, 0.5); + }; + expect(await same(scene, { expected, tolerance: 1 })).toBeGreaterThan( + 0, + ); + }); + + it("half transparent, its tiles overlapping: faded as one picture", async (ctx) => { + need(ctx); + // The GPU path draws the layer flat, then fades it: what is + // behind a tile does not show through it, as with a layer + // baked into one image ("prerender"). Tile by tile, each + // tile is blended on the ones behind it instead. + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), opacity: 0.5 }], + }); + const views = VIEWS.slice(0, 3); + const baked = []; + let layers = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + baked.push((await drawIn(app, layers, "prerender")).frame); + } + layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + for (const [i, [x, y]] of views.entries()) { + app.viewport.moveTo(x, y); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const flat = layered(reference(scene, x, y, WIDTH, HEIGHT), 0.5); + const d = diff(flat, gpu.frame, 1); + expect(d.different, `view (${x}, ${y}): ${d.first}`).toBe(0); + const b = diff(baked[i], gpu.frame, 1); + expect(b.different, `as prerender, view (${x}, ${y})`).toBe(0); + expect(drawn(gpu.frame)).toBeGreaterThan(0); + } + }); + + it("tinted", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), tintcolor: "#ff8040" }], + }); + const expected = (picture) => { + return layered(picture, 1, [255, 128, 64]); + }; + expect(await same(scene, { expected, tolerance: 1 })).toBeGreaterThan( + 0, + ); + }); + + it("one of several, each on a tileset of its own", async (ctx) => { + need(ctx); + // a floor of flat tiles under walls of tall ones + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 128)], + layers: [ + { + data: fill(8, 6, (c, r) => { + return 1 + ((c + r) % 8); + }), + }, + { + data: fill(8, 6, (c, r) => { + return (c + r) % 3 === 0 ? 9 + ((c * 2 + r) % 8) : 0; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("one of several, on tilesets of several sizes moved by a tileoffset", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [ + blocks(64, 32, { extra: { tileoffset: { x: 5, y: 9 } } }), + blocks(64, 128, { extra: { tileoffset: { x: -20, y: 40 } } }), + blocks(128, 64), + ], + layers: [ + { data: scattered(8, 6) }, + { data: scattered(8, 6, 8, 9) }, + { + data: fill(8, 6, (c, r) => { + return (c + r) % 4 === 1 ? 17 + ((c + r * 3) % 8) : 0; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("of an infinite map, starting left of and above the origin", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { + x, + y, + width: 4, + height: 4, + data: fill(4, 4, (c, r) => { + return (c + r + x) % 5 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }), + }; + }; + const scene = iso({ + cols: 8, + rows: 8, + tilesets: [blocks(64, 64)], + extra: { infinite: true }, + layers: [ + { + startx: -4, + starty: -4, + width: 12, + height: 8, + data: undefined, + chunks: [chunk(-4, -4), chunk(0, -4), chunk(4, 0), chunk(0, 0)], + }, + ], + }); + const layers = show(app, makeMap(scene)); + expect(layers[0].startx).toBe(-4); + expect(layers[0].renderMode).toBe("shader"); + const views = [ + [0, 0], + [-200, -150], + [150, -100], + [-60, 40], + ]; + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + }); + }); + + describe("isometric: what a camera can do", () => { + const tall = () => { + return iso({ + cols: 10, + rows: 8, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(10, 8) }], + }); + }; + + it("look from anywhere, and from where nothing shows", async (ctx) => { + need(ctx); + const scene = tall(); + const layers = show(app, makeMap(scene)); + const views = []; + for (let x = -400; x <= 700; x += 137) { + for (let y = -300; y <= 400; y += 97) { + views.push([x, y]); + } + } + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + // far off the map: nothing, either way + const far = [ + [5000, 5000], + [-5000, 0], + ]; + expect(await same(scene, { views: far, layers })).toBe(0); + }); + + it("scroll a pixel at a time, across a tile", async (ctx) => { + need(ctx); + const scene = tall(); + const layers = show(app, makeMap(scene)); + const views = []; + for (let step = 0; step <= 64; step += 3) { + views.push([40 + step, 10 + Math.floor(step / 2)]); + } + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + }); + + it("stand between pixels", async (ctx) => { + need(ctx); + // the camera's position is rounded to whole pixels when the + // world is drawn: the reference is painted from there + const scene = tall(); + const layers = show(app, makeMap(scene)); + for (const [x, y] of [ + [10.5, 7.25], + [-33.75, 60.5], + [120.1, -45.9], + ]) { + app.viewport.moveTo(x, y); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const d = diff(cpu.frame, gpu.frame); + expect(d.different, `view (${x}, ${y}): ${d.first}`).toBe(0); + expect(drawn(gpu.frame)).toBeGreaterThan(0); + } + }); + + it("be shifted by its offset, as a shake does", async (ctx) => { + need(ctx); + const scene = tall(); + app.viewport.offset.set(13, -9); + try { + expect(await same(scene, { oracle: false })).toBeGreaterThan(0); + } finally { + app.viewport.offset.set(0, 0); + } + }); + + for (const zoom of [2, 3]) { + it(`zoom ${zoom} times`, async (ctx) => { + need(ctx); + const scene = tall(); + app.viewport.zoom = zoom; + try { + expect(await same(scene, { oracle: false })).toBeGreaterThan(0); + } finally { + app.viewport.zoom = 1; + } + }); + } + + for (const zoom of [0.5, 1.5]) { + it(`zoom ${zoom} times, a texel apart at most but for a few pixels`, async (ctx) => { + need(ctx); + // Tiles are sampled without smoothing: where a screen + // pixel's centre falls exactly between two texels, a + // quad per tile and one quad for the layer do not round + // the same way. A texel apart is within the tolerance + // (colours change by 7 a texel at most here); the + // outlines of the tiles, where the next texel is another + // tile's, are the share left. An orthogonal layer's two + // paths are as far apart. + const scene = tall(); + app.viewport.zoom = zoom; + try { + expect( + await same(scene, { oracle: false, tolerance: 8, share: 0.06 }), + ).toBeGreaterThan(0); + } finally { + app.viewport.zoom = 1; + } + }); + } + }); + + describe("isometric: tiles changed while the game runs", () => { + /** set a cell in the world and in the scene the reference reads */ + const setCell = (scene, layer, col, row, gid) => { + scene.layers[0].data[row * scene.cols + col] = gid; + if (gid === 0) { + layer.clearTile(col, row); + } else { + layer.setTile(layer.getTileById(gid, col, row), col, row); + } + }; + + it("setTile and clearTile show at once", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6) }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + const options = { views: [[0, 0]], layers }; + await same(scene, options); + const before = (await drawIn(app, layers, "shader")).frame; + + setCell(scene, layer, 3, 2, 5); + setCell(scene, layer, 0, 5, 2 + FLIP_H); + await same(scene, options); + const set = (await drawIn(app, layers, "shader")).frame; + expect(diff(before, set).different).toBeGreaterThan(0); + + setCell(scene, layer, 3, 2, 0); + setCell(scene, layer, 7, 0, 0); + await same(scene, options); + const cleared = (await drawIn(app, layers, "shader")).frame; + expect(diff(set, cleared).different).toBeGreaterThan(0); + expect(layer.renderMode).toBe("shader"); + }); + + it("every cell changed, one frame after the other", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 5, + tilesets: [blocks(64, 96)], + layers: [{ data: scattered(6, 5) }], + }); + const layers = show(app, makeMap(scene)); + const options = { views: [[0, 0]], layers }; + for (let step = 0; step < 4; step++) { + for (let row = 0; row < 5; row++) { + for (let col = 0; col < 6; col++) { + const gid = + (col + row + step) % 4 === 0 ? 0 : 1 + ((col * step + row) % 8); + setCell(scene, layers[0], col, row, gid); + } + } + await same(scene, options); + } + expect(layers[0].renderMode).toBe("shader"); + }); + + it("an empty layer takes its tileset from the first tile set", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [ + { + data: fill(6, 6, () => { + return 0; + }), + }, + ], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer._shaderTileset).toBeUndefined(); + setCell(scene, layer, 2, 3, 12); + setCell(scene, layer, 3, 3, 10); + expect(layer.renderMode).toBe("shader"); + expect(layer._shaderTileset.firstgid).toBe(9); + expect(await same(scene, { views: [[0, 0]], layers })).toBeGreaterThan( + 0, + ); + }); + + it("a tile of a second tileset moves the layer back to the CPU renderer", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [{ data: scattered(6, 6) }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + // one of the same tileset: nothing changes + setCell(scene, layer, 1, 1, 3); + expect(layer.renderMode).toBe("shader"); + expect(warn).not.toHaveBeenCalled(); + // one of the other + setCell(scene, layer, 2, 2, 10); + expect(layer.renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /more than one tileset/, + ); + } finally { + warn.mockRestore(); + } + // and the layer is drawn right from there on + app.viewport.moveTo(0, 0); + app.world.update(0); + app.repaint(); + app.draw(); + const frame = await grabFrame(app.renderer); + const d = diff(reference(scene, 0, 0, WIDTH, HEIGHT), frame); + expect(d.different, d.first).toBe(0); + }); + + it("a tile that is not square, flipped diagonally, does too", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 96)], + layers: [{ data: scattered(6, 6) }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + setCell(scene, layer, 1, 1, 3 + FLIP_H + FLIP_V); + expect(layer.renderMode).toBe("shader"); + setCell(scene, layer, 2, 2, 4 + FLIP_AD); + expect(layer.renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /not square, flipped diagonally/, + ); + } finally { + warn.mockRestore(); + } + }); + }); + + describe("isometric: layers the GPU path does not take", () => { + it("tiles of two tilesets in one layer", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [ + { + data: fill(6, 6, (c, r) => { + return 1 + ((c + r) % 16); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/more than one tileset in an isometric layer/); + }); + + it("tiles past the size the shader walks", (ctx) => { + need(ctx); + const [tall, saidTall] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 257)], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(tall).toBe("perTile"); + expect(saidTall).toMatch(/larger than the shader's limit/); + const [wide] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [makeTileset({ tw: 257, th: 32 })], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(wide).toBe("perTile"); + }); + + it("a tileset drawn at the map's grid size", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 64, { extra: { tilerendersize: "grid" } })], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/tilerendersize/); + }); + + it("a tile that is not square, flipped diagonally", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 96)], + layers: [ + { + data: fill(4, 4, (c, r) => { + return 1 + ((c + r) % 8) + (c === 2 && r === 1 ? FLIP_AD : 0); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/not square, flipped diagonally/); + }); + + it("a grid of odd size, whose tiles stand half a pixel off", async (ctx) => { + need(ctx); + for (const [tw, th] of [ + [33, 17], + [64, 31], + [63, 32], + ]) { + const scene = { + orientation: "isometric", + cols: 9, + rows: 7, + tw, + th, + tilesets: [makeTileset({ tw, th: th * 2 })], + layers: [{ data: scattered(9, 7) }], + }; + const [mode, said] = modeOf(scene); + expect(mode, `${tw} × ${th}`).toBe("perTile"); + expect(said).toMatch(/isometric grid of odd size/); + } + }); + + it("a layer blended another way than the normal one", (ctx) => { + need(ctx); + for (const mode of ["add", "multiply", "screen"]) { + const [took, said] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(4, 4), mode }], + }), + ); + expect(took, mode).toBe("perTile"); + expect(said).toMatch(/blend mode/); + } + }); + + it("and one asked for by hand afterwards is still drawn, tile by tile", async (ctx) => { + need(ctx); + // two tilesets in one layer: the layer has no tileset to + // draw with on the GPU + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [ + { + data: fill(6, 6, (c, r) => { + return 1 + ((c + r) % 16); + }), + }, + ], + }); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + let layers; + try { + layers = show(app, makeMap(scene)); + } finally { + warn.mockRestore(); + } + expect(layers[0].renderMode).toBe("perTile"); + app.viewport.moveTo(0, 0); + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(1); + const d = diff(reference(scene, 0, 0, WIDTH, HEIGHT), forced.frame); + expect(d.different, d.first).toBe(0); + expect(drawn(forced.frame)).toBeGreaterThan(0); + }); + + it("any layer, with gpuTilemap off", (ctx) => { + need(ctx); + app.world.gpuTilemap = false; + try { + const [mode] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks()], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(mode).toBe("perTile"); + } finally { + app.world.gpuTilemap = true; + } + }); + }); + + describe("orthogonal: the same picture, both ways", () => { + const ortho = (options) => { + return { orientation: "orthogonal", tw: 32, th: 32, ...options }; + }; + const ORTHO_VIEWS = [ + [0, 0], + [40, 30], + [-50, -60], + [200, 150], + [-250, 20], + [90, -150], + ]; + + it("tiles the size of the grid, flipped every way", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 12, + rows: 10, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [ + { + data: fill(12, 10, (c, r) => { + return (c * 3 + r) % 5 === 0 + ? 0 + : 1 + ((c + r * 2) % 8) + ALL_FLIPS[(c + r) % 8]; + }), + }, + ], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + + // tiles larger than the grid stand on their cell and reach above + // it and to its right: above the map's first row and right of + // its last column too + for (const [tw, th] of [ + [32, 64], + [32, 128], + [64, 32], + [96, 96], + [48, 40], + ]) { + it(`${tw} × ${th} tiles on a 32 × 32 grid`, async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ + tw, + th, + opaque: (x, y) => { + return (x + y) % 9 !== 0 || x < 4; + }, + }), + ], + layers: [{ data: scattered(10, 8) }], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + } + + it("a tileset whose image has no margin after its last tiles", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ + tw: 32, + th: 32, + margin: 5, + spacing: 3, + outerMargin: false, + }), + ], + layers: [{ data: scattered(10, 8) }], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + + it("layers on tilesets of several sizes", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ + tw: 64, + th: 96, + opaque: (x, y) => { + return x > 10 && y > 6; + }, + }), + ], + layers: [ + { data: scattered(10, 8) }, + { + data: fill(10, 8, (c, r) => { + return (c + r * 2) % 4 === 0 ? 9 + ((c + r) % 8) : 0; + }), + }, + ], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + + it("tiles larger than the grid, with soft edges and see-through", async (ctx) => { + need(ctx); + for (const [tw, th] of [ + [64, 64], + [32, 96], + [80, 48], + ]) { + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ + tw, + th, + alpha: (x, y, id) => { + // a soft border, and every third tile + // see-through all over + const edge = Math.min(x, y, tw - 1 - x, th - 1 - y); + const a = id % 3 === 2 ? 110 : 255; + return Math.round(a * Math.min(1, (edge + 1) / 4)); + }, + }), + ], + layers: [{ data: scattered(10, 8) }], + }); + expect( + await same(scene, { views: ORTHO_VIEWS, tolerance: 3 }), + `${tw} × ${th}`, + ).toBeGreaterThan(0); + } + }); + + it("a tile that is not square, flipped diagonally, is left to the CPU renderer", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + ortho({ + cols: 4, + rows: 4, + tilesets: [makeTileset({ tw: 32, th: 64 })], + layers: [ + { + data: fill(4, 4, (c, r) => { + return 1 + ((c + r) % 8) + (c === 1 && r === 2 ? FLIP_AD : 0); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/not square, flipped diagonally/); + }); + }); + + describe("a layer moved, in Tiled or in the game", () => { + const ortho = (options) => { + return { orientation: "orthogonal", tw: 32, th: 32, ...options }; + }; + + /** the scene baked into one image ("prerender"), from each view */ + const baked = async (scene, views = VIEWS.slice(0, 4)) => { + const layers = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `baked, view (${x}, ${y}): ${d.first}`).toBe(0); + } + }; + + for (const [x, y] of [ + [37, -21], + [-130, 75], + [0, 200], + ]) { + it(`isometric, offset by (${x}, ${y}) in Tiled`, async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), offsetx: x, offsety: y }], + }); + expect(await same(scene)).toBeGreaterThan(0); + await baked(scene); + }); + + it(`orthogonal, offset by (${x}, ${y}) in Tiled`, async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 32, th: 64 })], + layers: [{ data: scattered(10, 8), offsetx: x, offsety: y }], + }); + expect(await same(scene)).toBeGreaterThan(0); + await baked(scene); + }); + } + + it("isometric layers, each with an offset of its own, on tilesets moved by a tileoffset", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [ + blocks(64, 32, { extra: { tileoffset: { x: 5, y: 9 } } }), + blocks(64, 128, { extra: { tileoffset: { x: -20, y: 40 } } }), + blocks(128, 64), + ], + layers: [ + { data: scattered(8, 6), offsetx: -10, offsety: 30 }, + { data: scattered(8, 6, 8, 9), offsetx: 64, offsety: 0 }, + { + data: fill(8, 6, (c, r) => { + return (c + r) % 4 === 1 ? 17 + ((c + r * 3) % 8) : 0; + }), + offsetx: 3, + offsety: -70, + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("orthogonal layers, each with an offset of its own", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ tw: 64, th: 96 }), + ], + layers: [ + { data: scattered(10, 8), offsetx: 16, offsety: 8 }, + { + data: fill(10, 8, (c, r) => { + return (c + r * 2) % 4 === 0 ? 9 + ((c + r) % 8) : 0; + }), + offsetx: -45, + offsety: 61, + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("a layer of an infinite isometric map, offset", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { + x, + y, + width: 4, + height: 4, + data: fill(4, 4, (c, r) => { + return (c + r + x) % 5 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }), + }; + }; + const scene = iso({ + cols: 8, + rows: 8, + tilesets: [blocks(64, 64)], + extra: { infinite: true }, + layers: [ + { + startx: -4, + starty: -4, + width: 12, + height: 8, + data: undefined, + chunks: [chunk(-4, -4), chunk(0, -4), chunk(4, 0), chunk(0, 0)], + offsetx: 48, + offsety: -36, + }, + ], + }); + const layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + const views = [ + [0, 0], + [-200, -150], + [150, -100], + ]; + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + }); + + for (const orientation of ["isometric", "orthogonal"]) { + it(`${orientation}: moved in the game, by its pos`, async (ctx) => { + need(ctx); + const layer0 = { offsetx: 12, offsety: 7 }; + const scene = + orientation === "isometric" + ? iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), ...layer0 }], + }) + : ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: scattered(10, 8), ...layer0 }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + // where Tiled put it + const startX = layer.pos.x; + const startY = layer.pos.y; + expect(await same(scene, { layers })).toBeGreaterThan(0); + for (const [dx, dy] of [ + [50, -30], + [-170, 90], + [0, 0], + ]) { + layer.pos.x = startX + dx; + layer.pos.y = startY + dy; + // the reference follows + scene.layers[0].offsetx = 12 + dx; + scene.layers[0].offsety = 7 + dy; + expect( + await same(scene, { layers }), + `moved by (${dx}, ${dy})`, + ).toBeGreaterThan(0); + } + }); + } + }); + + describe("tiles that reach past the grid, when only they are in view", () => { + const ortho = (options) => { + return { orientation: "orthogonal", tw: 32, th: 32, ...options }; + }; + const all = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return 1 + ((c + r * 2) % 8); + }); + }; + + /** the scene baked into one image ("prerender"), from each view */ + const baked = async (scene, views) => { + const layers = show(app, makeMap(scene), "prerender"); + let total = 0; + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `baked, view (${x}, ${y}): ${d.first}`).toBe(0); + total += drawn(frame); + } + return total; + }; + + it("isometric: tall tiles, above the map's top corner", async (ctx) => { + need(ctx); + // the 64 × 128 tiles of the first rows reach 96 pixels above + // the grid, which starts at 0: these views end above it + const scene = iso({ + cols: 10, + rows: 8, + tilesets: [blocks(64, 128)], + layers: [{ data: all(10, 8) }], + }); + const views = [ + [160, -HEIGHT - 10], + [100, -HEIGHT - 40], + [220, -HEIGHT - 70], + [-126, -300], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + expect(await baked(scene, views)).toBeGreaterThan(0); + }); + + it("orthogonal: tall tiles, above the first row", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 32, th: 128 })], + layers: [{ data: all(10, 8) }], + }); + const views = [ + [0, -HEIGHT - 10], + [40, -HEIGHT - 50], + [-200, -HEIGHT - 80], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + expect(await baked(scene, views)).toBeGreaterThan(0); + }); + + it("orthogonal: wide tiles, right of the last column", async (ctx) => { + need(ctx); + // 96 pixels wide on a 32 pixel grid: 64 pixels past the map, + // which ends at 320 + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 96, th: 32 })], + layers: [{ data: all(10, 8) }], + }); + const views = [ + [325, 0], + [350, 40], + [380, -100], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + expect(await baked(scene, views)).toBeGreaterThan(0); + }); + + it("isometric tiles moved off the grid by their tileset's tileoffset", async (ctx) => { + need(ctx); + // moved 150 pixels up and to the left: where the grid is not + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [ + blocks(64, 64, { extra: { tileoffset: { x: -150, y: -150 } } }), + ], + layers: [{ data: all(6, 6) }], + }); + const views = [ + [-WIDTH - 10, 0], + [-100, -HEIGHT - 20], + [-WIDTH - 60, -80], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + }); + + it("orthogonal tiles moved off the grid by their tileset's tileoffset", async (ctx) => { + need(ctx); + // under the map, which ends at 192. An orthogonal tileoffset + // leaves the layer to the CPU renderer: it is drawn as well + const scene = ortho({ + cols: 8, + rows: 6, + tilesets: [ + makeTileset({ + tw: 32, + th: 32, + extra: { tileoffset: { x: 0, y: 90 } }, + }), + ], + layers: [{ data: all(8, 6) }], + }); + const [mode] = modeOf(scene); + expect(mode).toBe("perTile"); + const layers = app.world.getChildren().filter((child) => { + return child.layerData !== undefined; + }); + for (const [x, y] of [ + [0, 200], + [-40, 250], + ]) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "perTile"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `view (${x}, ${y}): ${d.first}`).toBe(0); + expect(drawn(frame)).toBeGreaterThan(0); + } + }); + }); + + it("a level change frees what the GPU path kept for its layers", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 64)], + layers: [{ data: scattered(6, 6) }], + }); + const layers = show(app, makeMap(scene)); + const options = { views: [[0, 0]], layers }; + await same(scene, options); + const gpu = + app.renderer._tmxGPURenderers?.get("isometric") ?? + app.renderer.tmxRenderers?.get("isometric"); + expect(gpu.resources.size).toBe(1); + app.renderer.reset(); + expect(gpu.resources.size).toBe(0); + // and the layer draws again after it + expect(await same(scene, options)).toBeGreaterThan(0); + }); + }); +} diff --git a/packages/melonjs/tests/helpers/tile-layer-placement-checks.js b/packages/melonjs/tests/helpers/tile-layer-placement-checks.js new file mode 100644 index 0000000000..ddcf86d3f7 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-placement-checks.js @@ -0,0 +1,277 @@ +/** + * A tile layer drawn where its ancestors put it: in a world moved to centre + * a map smaller than the view (what `level.load` does), or in a container + * that was moved. The layer is handed the camera's view in the world's + * coordinates, and has to read it in its own: the tiles that reach above or + * left of its grid were cut on the GPU and baked paths, and a layer far + * enough from the origin lost tiles tile by tile. + * + * Also here: a layer's `renderMode` switched by hand, and tiles set in + * between. + * + * One spec file per renderer calls this: an application takes the place of + * the one before it, and a spec file has its own page. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { Container } from "../../src/index.js"; +import { + comparePaths, + diff, + drawIn, + drawn, + FLIP_AD, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +const WIDTH = 320; +const HEIGHT = 240; + +const all = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return 1 + ((c + r * 2) % 8); + }); +}; + +/** small maps of tiles that reach past their grid, one per orientation */ +const SCENES = { + orthogonal: () => { + return { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 48, th: 64 })], + layers: [{ data: all(4, 3) }], + }; + }, + isometric: () => { + return { + orientation: "isometric", + cols: 3, + rows: 3, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th: 96, + opaque: (x, y) => { + return inBlock(x, y, 64, 96, 64, 32); + }, + }), + ], + layers: [{ data: all(3, 3) }], + }; + }, + staggered: () => { + return { + orientation: "staggered", + cols: 3, + rows: 5, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th: 80, + extra: { tileoffset: { x: -20, y: 0 } }, + }), + ], + layers: [{ data: all(3, 5) }], + extra: { staggeraxis: "y", staggerindex: "odd" }, + }; + }, + hexagonal: () => { + return { + orientation: "hexagonal", + cols: 5, + rows: 5, + tw: 28, + th: 24, + tilesets: [makeTileset({ tw: 28, th: 60 })], + layers: [{ data: all(5, 5) }], + extra: { staggeraxis: "x", staggerindex: "even", hexsidelength: 12 }, + }; + }, +}; + +const VIEWS = [ + [0, 0], + [30, 20], + [-45, -60], +]; + +/** + * Declare the checks for one renderer. + * @param {number} renderer - `video.CANVAS`, `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected + * @param {string[]} modes - the render modes that renderer has + */ +export function placementChecks(renderer, type, modes) { + describe(`a tile layer where its ancestors put it (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, WIDTH, HEIGHT); + }); + afterAll(() => { + app?.world.pos.set(0, 0, 0); + app?.destroy(); + vi.restoreAllMocks(); + }); + + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + + for (const [orientation, build] of Object.entries(SCENES)) { + it(`${orientation}: a map centred in the view keeps the tiles that reach past its grid`, async (ctx) => { + need(ctx); + const scene = build(); + let total = 0; + for (const mode of modes) { + const layers = show( + app, + makeMap(scene), + mode === "prerender" ? mode : undefined, + true, + ); + const { x: ox, y: oy } = app.world.pos; + // smaller than the view: centred, by moving the world + expect(ox, "centred across").toBeGreaterThan(0); + expect(oy, "centred down").toBeGreaterThan(0); + for (const [x, y] of VIEWS) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, mode); + const ref = reference(scene, x - ox, y - oy, WIDTH, HEIGHT); + const d = diff(ref, frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe( + 0, + ); + total += drawn(frame); + } + } + expect(total).toBeGreaterThan(0); + }); + + for (const [gx, gy] of [ + [120, 80], + [-700, 900], + ]) { + it(`${orientation}: in a container moved to (${gx}, ${gy})`, async (ctx) => { + need(ctx); + const scene = build(); + let total = 0; + for (const mode of modes) { + app.world.reset(); + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + const map = makeMap(scene); + const layers = map.getLayers(); + if (mode === "prerender") { + layers[0].renderMode = mode; + } + const group = new Container(); + group.pos.set(gx, gy, 0); + app.world.addChild(group); + map.addTo(group, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + for (const [x, y] of VIEWS) { + // the view follows the container + app.viewport.moveTo(x + gx, y + gy); + const { frame } = await drawIn(app, layers, mode); + const ref = reference(scene, x, y, WIDTH, HEIGHT); + const d = diff(ref, frame); + expect( + d.different, + `${mode}, view (${x}, ${y}): ${d.first}`, + ).toBe(0); + total += drawn(frame); + } + } + expect(total).toBeGreaterThan(0); + }); + } + } + + if (type !== "CANVAS") { + it("both paths, centred: the same as the reference", async (ctx) => { + need(ctx); + for (const build of Object.values(SCENES)) { + const scene = build(); + const layers = show(app, makeMap(scene), undefined, true); + expect(layers[0].renderMode).toBe("shader"); + const origin = [app.world.pos.x, app.world.pos.y]; + expect( + await comparePaths(app, scene, { views: VIEWS, layers, origin }), + scene.orientation, + ).toBeGreaterThan(0); + } + }); + + describe("a render mode switched by hand, and tiles set in between", () => { + const two = (orientation) => { + const scene = SCENES[orientation](); + const [first] = scene.tilesets; + scene.tilesets = [ + first, + makeTileset({ tw: first.tilewidth, th: first.tileheight }), + ]; + return scene; + }; + + for (const orientation of ["isometric", "staggered", "hexagonal"]) { + it(`${orientation}: a tile of another tileset set while the layer was drawn tile by tile`, async (ctx) => { + need(ctx); + const scene = two(orientation); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + layer.renderMode = "perTile"; + // no warning: nothing is moved off the GPU path now + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + scene.layers[0].data[1 * scene.cols + 1] = 11; + layer.setTile(layer.getTileById(11, 1, 1), 1, 1); + expect(warn).not.toHaveBeenCalled(); + warn.mockRestore(); + // asked for the GPU path again: the layer has tiles of + // two tilesets, and is drawn tile by tile, whole + app.viewport.moveTo(0, 0); + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(1); + const d = diff(reference(scene, 0, 0, WIDTH, HEIGHT), forced.frame); + expect(d.different, d.first).toBe(0); + }); + } + + it("orthogonal: a tile not square, flipped diagonally, set while the layer was drawn tile by tile", async (ctx) => { + need(ctx); + const scene = SCENES.orthogonal(); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + app.viewport.moveTo(0, 0); + layer.renderMode = "perTile"; + layer.setTile(layer.getTileById(3 + FLIP_AD, 1, 1), 1, 1); + const cpu = await drawIn(app, layers, "perTile"); + // asked for the GPU path again: still tile by tile, and the + // same picture + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(1); + expect(diff(cpu.frame, forced.frame).different).toBe(0); + expect(drawn(forced.frame)).toBeGreaterThan(0); + }); + }); + } + }); +} diff --git a/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js new file mode 100644 index 0000000000..18d3d12399 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js @@ -0,0 +1,586 @@ +/** + * Staggered and hexagonal tile layers, held to the picture Tiled draws: the + * scenes, and for a GPU backend the same comparison as the isometric ones + * (`tile-layer-isometric-scenarios.js`): the CPU renderer, the GPU one and + * a reference painted with no engine, pixel by pixel. + * + * Both orientations are one geometry: lines of cells, every other one + * shifted by half a cell, along the map's rows (`staggeraxis` "y") or its + * columns ("x"); a hexagonal map's cells are longer by `hexsidelength`. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { + comparePaths, + diff, + drawIn, + drawn, + FLIP_AD, + FLIP_H, + FLIP_V, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +export const WIDTH = 320; +export const HEIGHT = 240; + +const COLS = 9; +const ROWS = 11; + +/** most cells filled, a few holes */ +const scattered = (cols = COLS, rows = ROWS, firstgid = 1) => { + return fill(cols, rows, (c, r) => { + return (c * 3 + r) % 7 === 0 ? 0 : firstgid + ((c + r * 2) % 8); + }); +}; + +/** the grids: a staggered map's diamonds, and hexagons either way up */ +const GRIDS = [ + { orientation: "staggered", tw: 64, th: 32 }, + { orientation: "hexagonal", tw: 28, th: 24, hexsidelength: 12 }, +]; + +/** + * A staggered or hexagonal scene. + * @param {object} grid - one of `GRIDS` + * @param {string} staggeraxis - "x" or "y" + * @param {string} staggerindex - "odd" or "even" + * @param {object} tileset - from `makeTileset` + * @param {object} [more] - what else the scene has (its layers, by default + * one of `scattered` cells) + * @returns {object} the scene, for `makeMap` and `reference` + */ +export const staggeredScene = ( + grid, + staggeraxis, + staggerindex, + tileset, + more = {}, +) => { + const { orientation, tw, th, hexsidelength } = grid; + return { + orientation, + cols: COLS, + rows: ROWS, + tw, + th, + tilesets: [tileset], + layers: [{ data: scattered() }], + ...more, + extra: { staggeraxis, staggerindex, hexsidelength, ...more.extra }, + }; +}; + +/** the kinds of tiles each grid is drawn with */ +const TILES = [ + { + label: "tiles the size of a cell, filling it: every overlap shows", + make: ({ tw, th }) => { + return makeTileset({ tw, th }); + }, + }, + { + label: "tiles taller than a cell, standing on it", + make: ({ tw, th }) => { + return makeTileset({ + tw, + th: th * 3, + opaque: (x, y) => { + return inBlock(x, y, tw, th * 3, tw, th); + }, + }); + }, + }, + { + label: "tiles wider and taller than a cell, moved by a tileoffset", + make: ({ tw, th }) => { + return makeTileset({ + tw: tw + 20, + th: th * 2 + 6, + opaque: (x, y) => { + return (x + y) % 11 !== 0; + }, + extra: { tileoffset: { x: -6, y: 10 } }, + }); + }, + }, +]; + +/** every grid, either axis, either index, each kind of tiles */ +export const STAGGERED_SCENES = []; +for (const grid of GRIDS) { + for (const staggeraxis of ["y", "x"]) { + for (const staggerindex of ["odd", "even"]) { + for (const tiles of TILES) { + STAGGERED_SCENES.push({ + label: `${grid.orientation}, axis ${staggeraxis}, ${staggerindex}: ${tiles.label}`, + scene: () => { + return staggeredScene( + grid, + staggeraxis, + staggerindex, + tiles.make(grid), + ); + }, + }); + } + } + } +} + +export const STAGGERED_VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + [-150, 30], + [100, -200], + [33, 57], +]; + +/** + * Declare the scenarios for one GPU backend. + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected ("WebGL", "WebGPU") + */ +export function staggeredScenarios(renderer, type) { + describe(`staggered and hexagonal tile layers on the GPU, against the CPU renderer and a reference (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + const same = (scene, options = {}) => { + return comparePaths(app, scene, { views: STAGGERED_VIEWS, ...options }); + }; + const modeOf = (scene) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layer = show(app, makeMap(scene))[0]; + return [layer.renderMode, warn.mock.calls.flat().join(" ")]; + } finally { + warn.mockRestore(); + } + }; + const [STAGGERED, HEXAGONAL] = GRIDS; + const blocks = (grid, tall = 2, options = {}) => { + return makeTileset({ + tw: grid.tw, + th: grid.th * tall, + opaque: (x, y) => { + return inBlock(x, y, grid.tw, grid.th * tall, grid.tw, grid.th); + }, + ...options, + }); + }; + + describe("every grid, either way", () => { + for (const { label, scene } of STAGGERED_SCENES) { + it(label, async (ctx) => { + need(ctx); + expect(await same(scene())).toBeGreaterThan(0); + }); + } + }); + + describe("what a tile and a layer can be", () => { + it("tiles with soft edges, and see-through: each shows the tiles behind it", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const tileset = makeTileset({ + tw: grid.tw, + th: grid.th * 2, + alpha: (x, y, id) => { + const edge = Math.min(x, y, grid.tw - 1 - x, grid.th * 2 - 1 - y); + const a = id % 3 === 2 ? 110 : 255; + return Math.round(a * Math.min(1, (edge + 1) / 4)); + }, + }); + for (const axis of ["y", "x"]) { + expect( + await same(staggeredScene(grid, axis, "odd", tileset), { + tolerance: 3, + }), + `${grid.orientation}, axis ${axis}`, + ).toBeGreaterThan(0); + } + } + }); + + it("tiles of odd size: on the cell Tiled lays out, a pixel smaller", async (ctx) => { + need(ctx); + // 63 x 31 tiles of a staggered map stand on 62 x 30 cells; + // a hexagon side of 11 on 28 x 25 leaves 7 a side, rows 18 + // apart + for (const [grid, tw, th] of [ + [{ orientation: "staggered", tw: 63, th: 31 }, 63, 31], + [{ orientation: "staggered", tw: 64, th: 33 }, 64, 66], + [ + { orientation: "hexagonal", tw: 28, th: 25, hexsidelength: 11 }, + 28, + 25, + ], + [ + { orientation: "hexagonal", tw: 27, th: 24, hexsidelength: 12 }, + 27, + 40, + ], + ]) { + for (const axis of ["y", "x"]) { + expect( + await same( + staggeredScene(grid, axis, "odd", makeTileset({ tw, th })), + ), + `${grid.orientation} ${grid.tw} x ${grid.th}, axis ${axis}`, + ).toBeGreaterThan(0); + } + } + }); + + it("tiles flipped across and down, on hexagons", async (ctx) => { + need(ctx); + // (a hexagonal map's third flag is a turn of 60 degrees in + // Tiled, which the engine does not draw: left out here) + const flips = [0, FLIP_H, FLIP_V, FLIP_H + FLIP_V]; + for (const axis of ["y", "x"]) { + const scene = staggeredScene( + HEXAGONAL, + axis, + "even", + makeTileset({ + tw: 28, + th: 40, + opaque: (x, y) => { + return x + y > 12; + }, + }), + { + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r) % 8) + flips[(c * 3 + r) % 4]; + }), + }, + ], + }, + ); + expect(await same(scene), `axis ${axis}`).toBeGreaterThan(0); + } + }); + + it("tiles flipped, every way", async (ctx) => { + need(ctx); + const flips = [ + 0, + FLIP_H, + FLIP_V, + FLIP_H + FLIP_V, + FLIP_AD, + FLIP_H + FLIP_AD, + FLIP_V + FLIP_AD, + FLIP_H + FLIP_V + FLIP_AD, + ]; + // square tiles: a diagonal flip turns the image a quarter + const tileset = makeTileset({ + tw: 64, + th: 64, + opaque: (x, y) => { + return x + y > 20; + }, + }); + const scene = staggeredScene(STAGGERED, "y", "odd", tileset, { + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r) % 8) + flips[(c * 3 + r) % 8]; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("animated tiles, as time passes", async (ctx) => { + need(ctx); + const tileset = blocks(STAGGERED, 2, { + extra: { + tiles: [ + { + id: 0, + animation: [ + { tileid: 0, duration: 100 }, + { tileid: 5, duration: 100 }, + ], + }, + ], + }, + }); + const layers = show( + app, + makeMap(staggeredScene(STAGGERED, "x", "even", tileset)), + ); + expect(layers[0].renderMode).toBe("shader"); + app.viewport.moveTo(0, 0); + const frames = []; + for (let step = 0; step < 4; step++) { + const cpu = await drawIn(app, layers, "perTile", 60); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const d = diff(cpu.frame, gpu.frame); + expect(d.different, `step ${step}: ${d.first}`).toBe(0); + frames.push(gpu.frame); + } + expect(diff(frames[0], frames[2]).different).toBeGreaterThan(0); + }); + + it("a layer offset in Tiled, and moved in the game", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const scene = staggeredScene(grid, "y", "even", blocks(grid), { + layers: [{ data: scattered(), offsetx: 37, offsety: -21 }], + }); + const layers = show(app, makeMap(scene)); + expect(await same(scene, { layers })).toBeGreaterThan(0); + layers[0].pos.x += 50; + layers[0].pos.y += 64; + scene.layers[0].offsetx += 50; + scene.layers[0].offsety += 64; + expect(await same(scene, { layers })).toBeGreaterThan(0); + } + }); + + it("two layers, each on a tileset of its own", async (ctx) => { + need(ctx); + const scene = staggeredScene( + STAGGERED, + "y", + "odd", + blocks(STAGGERED, 1), + { + tilesets: [blocks(STAGGERED, 1), blocks(STAGGERED, 3)], + layers: [ + { data: scattered() }, + { + data: fill(COLS, ROWS, (c, r) => { + return (c + r) % 3 === 0 ? 9 + ((c * 2 + r) % 8) : 0; + }), + }, + ], + }, + ); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("a layer of an infinite map, starting left of and above the origin", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { + x, + y, + width: 4, + height: 4, + data: fill(4, 4, (c, r) => { + return (c + r + x) % 5 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }), + }; + }; + for (const grid of GRIDS) { + for (const axis of ["y", "x"]) { + const scene = staggeredScene(grid, axis, "odd", blocks(grid), { + cols: 8, + rows: 8, + layers: [ + { + startx: -4, + starty: -4, + width: 12, + height: 8, + data: undefined, + chunks: [ + chunk(-4, -4), + chunk(0, -4), + chunk(4, 0), + chunk(0, 0), + // odd coordinates: the lines that are shifted + // are the map's, not the chunk's + chunk(-3, 5), + chunk(5, -9), + ], + }, + ], + extra: { infinite: true }, + }); + const layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + const views = [ + [0, 0], + [-200, -150], + [150, -60], + ]; + expect( + await same(scene, { views, layers }), + `${grid.orientation}, axis ${axis}`, + ).toBeGreaterThan(0); + } + } + }); + + it("tiles set and cleared while the game runs", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const scene = staggeredScene(grid, "x", "odd", blocks(grid, 3)); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + const options = { views: [[0, 0]], layers }; + for (let step = 0; step < 3; step++) { + for (let row = 0; row < ROWS; row++) { + for (let col = 0; col < COLS; col++) { + const gid = + (col + row + step) % 4 === 0 + ? 0 + : 1 + ((col * step + row) % 8); + scene.layers[0].data[row * COLS + col] = gid; + if (gid === 0) { + layer.clearTile(col, row); + } else { + layer.setTile(layer.getTileById(gid, col, row), col, row); + } + } + } + await same(scene, options); + } + expect(layer.renderMode).toBe("shader"); + } + }); + + it("views that hold only what reaches past the grid", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + // tiles four cells tall stand three cells above the grid + const scene = staggeredScene(grid, "y", "odd", blocks(grid, 4), { + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r * 2) % 8); + }), + }, + ], + }); + const views = [ + [0, -HEIGHT - 5], + [60, -HEIGHT - grid.th], + ]; + expect( + await same(scene, { views }), + grid.orientation, + ).toBeGreaterThan(0); + } + }); + }); + + describe("layers the GPU path does not take", () => { + it("tiles of two tilesets in one layer", (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const [mode, said] = modeOf( + staggeredScene(grid, "y", "odd", blocks(grid), { + tilesets: [blocks(grid), blocks(grid, 3)], + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r) % 16); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch( + new RegExp( + `more than one tileset in an? ${grid.orientation} layer`, + ), + ); + } + }); + + it("tiles past the size the shader walks", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + staggeredScene(STAGGERED, "y", "odd", blocks(STAGGERED, 9)), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/larger than the shader's limit/); + }); + + it("a tile of another tileset set while the game runs", (ctx) => { + need(ctx); + const scene = staggeredScene(HEXAGONAL, "y", "odd", blocks(HEXAGONAL), { + tilesets: [blocks(HEXAGONAL), blocks(HEXAGONAL, 2)], + }); + const layer = show(app, makeMap(scene))[0]; + expect(layer.renderMode).toBe("shader"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + layer.setTile(layer.getTileById(10, 2, 2), 2, 2); + expect(layer.renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /more than one tileset/, + ); + } finally { + warn.mockRestore(); + } + }); + + it("an oblique map, still", (ctx) => { + need(ctx); + const [mode, said] = modeOf({ + orientation: "oblique", + cols: 4, + rows: 4, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: scattered(4, 4) }], + extra: { skewx: 10, skewy: 0 }, + }); + expect(mode).toBe("perTile"); + expect(said).toMatch(/no gpu renderer supported yet for "oblique"/); + }); + }); + + it("and each is drawn the same baked into one image", async (ctx) => { + need(ctx); + for (const { label, scene: build } of STAGGERED_SCENES.filter((_, i) => { + return i % 5 === 1; + })) { + const scene = build(); + const layers = show(app, makeMap(scene), "prerender"); + let total = 0; + for (const [x, y] of STAGGERED_VIEWS.slice(0, 4)) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `${label}, view (${x}, ${y}): ${d.first}`).toBe( + 0, + ); + total += drawn(frame); + } + expect(total, label).toBeGreaterThan(0); + } + }); + }); +} diff --git a/packages/melonjs/tests/mask-effect-image-webgpu.spec.js b/packages/melonjs/tests/mask-effect-image-webgpu.spec.js new file mode 100644 index 0000000000..68aa4eb03e --- /dev/null +++ b/packages/melonjs/tests/mask-effect-image-webgpu.spec.js @@ -0,0 +1,67 @@ +/** + * The `MaskEffect` drawing checks (#1399) on WebGPU, in a spec file of its + * own: an application with a GPU device takes the place of the one before + * it, which the Canvas and WebGL checks of `mask-effect-image.spec.js` use. + * + * Skipped, visibly, where the browser has no WebGPU adapter. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { Application, boot, Camera2d, video } from "../src/index.js"; +import { + at, + discWithHole, + drawingChecks, + drawOverWhite, + SIZE, +} from "./helpers/mask-effect-checks.js"; +import { drawOffscreen } from "./helpers/pixels.js"; + +describe("MaskEffect: image and sprite shapes, on WebGPU", () => { + let app; + let available = false; + beforeAll(async () => { + boot(); + try { + app = new Application(SIZE, SIZE, { + parent: "screen", + renderer: video.WEBGPU, + consoleHeader: false, + }); + await app.init(); + available = app.renderer.type === "WebGPU"; + if (available) { + const renderer = drawOffscreen(app.renderer); + // the checks draw without a camera, which is what sets the + // projection in a game + renderer.projectionMatrix.ortho(0, SIZE, SIZE, 0, -1000, 1000); + renderer.setProjection(renderer.projectionMatrix); + } + } catch { + available = false; + } + }); + afterAll(() => { + app?.destroy(); + }); + + const getRenderer = (ctx) => { + if (!available) { + ctx.skip("WebGPU not available in this environment"); + } + return app.renderer; + }; + + drawingChecks(getRenderer); + + it("destroy frees the copy of the image uploaded to the GPU", async (ctx) => { + const renderer = getRenderer(ctx); + const effect = at(new Camera2d(0, 0, SIZE, SIZE), 0.5, { + shape: discWithHole(), + }); + await drawOverWhite(renderer, effect); + const canvas = effect._cover.canvas; + expect(renderer.cache.has(canvas)).toBe(true); + effect.destroy(); + expect(renderer.cache.has(canvas)).toBe(false); + }); +}); diff --git a/packages/melonjs/tests/mask-effect-image.spec.js b/packages/melonjs/tests/mask-effect-image.spec.js index e1f8cafd6e..be070555de 100644 --- a/packages/melonjs/tests/mask-effect-image.spec.js +++ b/packages/melonjs/tests/mask-effect-image.spec.js @@ -32,258 +32,21 @@ import { video, } from "../src/index.js"; import GLTFScene from "../src/level/gltf/GLTFScene.js"; -import { readPixel } from "./helpers/pixels.js"; +import { + at, + discWithHole, + drawingChecks, + drawOverWhite, + leftHalf, + offOrigin, + SIZE, + topLeftQuarter, +} from "./helpers/mask-effect-checks.js"; import { getWebGLRenderer, releaseWebGLRenderer, } from "./helpers/webgl-context.js"; -const SIZE = 64; - -/** a 32 px disc, radius 14, with a hole of radius 4 at its centre */ -const discWithHole = () => { - const canvas = document.createElement("canvas"); - canvas.width = canvas.height = 32; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#3a7"; - ctx.beginPath(); - ctx.arc(16, 16, 14, 0, Math.PI * 2); - ctx.fill(); - ctx.globalCompositeOperation = "destination-out"; - ctx.beginPath(); - ctx.arc(16, 16, 4, 0, Math.PI * 2); - ctx.fill(); - return canvas; -}; - -/** an image whose left half only is opaque */ -const leftHalf = (w = 20, h = 30) => { - const canvas = document.createElement("canvas"); - canvas.width = w; - canvas.height = h; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#f00"; - ctx.fillRect(0, 0, w / 2, h); - return canvas; -}; - -/** a 32 px U: its middle is open to the outside, so not part of it */ -const uShape = () => { - const canvas = document.createElement("canvas"); - canvas.width = canvas.height = 32; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#000"; - ctx.fillRect(2, 2, 8, 28); - ctx.fillRect(22, 2, 8, 28); - ctx.fillRect(2, 22, 28, 8); - return canvas; -}; - -/** an image whose top left quarter only is opaque */ -const topLeftQuarter = (w = 20, h = 30) => { - const canvas = document.createElement("canvas"); - canvas.width = w; - canvas.height = h; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#0f0"; - ctx.fillRect(0, 0, w / 2, h / 2); - return canvas; -}; - -/** a unit square that does not surround its origin */ -const offOrigin = () => { - return new Polygon(0, 0, [ - { x: 0.2, y: 0.2 }, - { x: 1, y: 0.2 }, - { x: 1, y: 1 }, - { x: 0.2, y: 1 }, - ]); -}; - -/** an effect at a given progress (1 = all visible, 0 = all covered) */ -const at = (camera, progress, options) => { - const effect = new MaskEffect(camera, { - color: "#000", - duration: 1000, - direction: "hide", - ...options, - }); - effect.tween.stop(); - effect.progress.value = progress; - return effect; -}; - -/** draw an effect over a white screen */ -const drawOverWhite = (renderer, effect) => { - renderer.save(); - renderer.resetTransform(); - renderer.setColor("#ffffff"); - renderer.fillRect(0, 0, SIZE, SIZE); - renderer.restore(); - effect.draw(renderer, SIZE, SIZE); - renderer.flush(); -}; - -/** 1 where a pixel is covered (dark), 0 where it is visible (light) */ -const covered = async (renderer, x, y) => { - return (await readPixel(renderer, x, y))[0] < 128 ? 1 : 0; -}; - -const CORNERS = [ - [1, 1], - [SIZE - 2, 1], - [1, SIZE - 2], - [SIZE - 2, SIZE - 2], -]; - -/** the same drawing checks, on one renderer */ -const drawingChecks = (getRenderer) => { - const camera = new Camera2d(0, 0, SIZE, SIZE); - - it("an image: the scene shows through its silhouette, hole filled in", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.3, { shape: discWithHole() }); - drawOverWhite(renderer, effect); - // the centre is the image's hole: part of the silhouette, so visible - expect(await covered(renderer, 32, 32)).toBe(0); - expect(await covered(renderer, 32, 26)).toBe(0); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(1); - } - effect.destroy(); - }); - - it("an image: covers everything once closed", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0, { shape: discWithHole() }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 32, 32)).toBe(1); - expect(await covered(renderer, 1, 1)).toBe(1); - effect.destroy(); - }); - - it("an image: just before the end, the whole screen is already uncovered", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.995, { shape: discWithHole() }); - drawOverWhite(renderer, effect); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - effect.destroy(); - }); - - it("a polygon: just before the end, the whole screen is already uncovered", async (ctx) => { - const renderer = getRenderer(ctx); - const diamond = new Polygon(0, 0, [ - { x: 0, y: -1 }, - { x: 1, y: 0 }, - { x: 0, y: 1 }, - { x: -1, y: 0 }, - ]); - const effect = at(camera, 0.995, { shape: diamond }); - drawOverWhite(renderer, effect); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - effect.destroy(); - }); - - it("inverted: the silhouette is the cover, the scene shows round it", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.7, { shape: discWithHole(), invert: true }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 32, 32)).toBe(1); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - effect.destroy(); - }); - - it("inverted: covers everything once closed, nothing at the start", async (ctx) => { - const renderer = getRenderer(ctx); - const closed = at(camera, 0, { shape: discWithHole(), invert: true }); - drawOverWhite(renderer, closed); - expect(await covered(renderer, 1, 1)).toBe(1); - expect(await covered(renderer, 32, 32)).toBe(1); - closed.destroy(); - const open = at(camera, 1, { shape: discWithHole(), invert: true }); - drawOverWhite(renderer, open); - expect(await covered(renderer, 32, 32)).toBe(0); - open.destroy(); - }); - - it("inverted geometry: an iris filled, growing out of the centre", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.7, { - shape: new Ellipse(0, 0, 1, 1), - invert: true, - }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 32, 32)).toBe(1); - expect(await covered(renderer, 1, 1)).toBe(0); - effect.destroy(); - }); - - it("an image open in its middle (a U): grows about its thickest part, and still uncovers everything", async (ctx) => { - const renderer = getRenderer(ctx); - // just after the hide starts, the middle of the screen is still seen - const start = at(camera, 0.99, { shape: uShape() }); - drawOverWhite(renderer, start); - expect(await covered(renderer, 32, 32)).toBe(0); - start.destroy(); - // and just before a reveal ends, nothing is covered - const end = at(camera, 0.995, { shape: uShape() }); - drawOverWhite(renderer, end); - for (const [x, y] of [...CORNERS, [32, 32], [32, 3]]) { - expect(await covered(renderer, x, y)).toBe(0); - } - end.destroy(); - }); - - it("a polygon off its origin: still uncovers everything, and inverted still covers it", async (ctx) => { - const renderer = getRenderer(ctx); - const open = at(camera, 0.995, { shape: offOrigin() }); - drawOverWhite(renderer, open); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - open.destroy(); - // the cover nearly grown: the screen is covered by the polygon itself - const cover = at(camera, 0.005, { shape: offOrigin(), invert: true }); - drawOverWhite(renderer, cover); - for (const [x, y] of [...CORNERS, [32, 32]]) { - expect(await covered(renderer, x, y)).toBe(1); - } - cover.destroy(); - }); - - it("nearly closed, the image is still drawn, a sliver of the scene in the middle", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.05, { shape: discWithHole() }); - const drawImage = vi.spyOn(renderer, "drawImage"); - drawOverWhite(renderer, effect); - expect(drawImage).toHaveBeenCalled(); - drawImage.mockRestore(); - expect(await covered(renderer, 32, 32)).toBe(0); - for (const [x, y] of [...CORNERS, [32, 20], [44, 32]]) { - expect(await covered(renderer, x, y)).toBe(1); - } - effect.destroy(); - }); - - it("center: closes on that point, not on the centre of the screen", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.2, { - shape: discWithHole(), - center: { x: 12, y: 12 }, - }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 12, 12)).toBe(0); - expect(await covered(renderer, 52, 52)).toBe(1); - effect.destroy(); - }); -}; - // One Canvas application for the whole file, made first: the engine gives a // viewport only to the application live at the first `init()` let app; @@ -333,7 +96,7 @@ describe("MaskEffect: image and sprite shapes", () => { return renderer; }); - it("destroy frees the copy of the image uploaded to the GPU", (ctx) => { + it("destroy frees the copy of the image uploaded to the GPU", async (ctx) => { if (renderer === undefined) { ctx.skip("WebGL renderer not available in this environment"); return; @@ -341,40 +104,13 @@ describe("MaskEffect: image and sprite shapes", () => { const effect = at(new Camera2d(0, 0, SIZE, SIZE), 0.5, { shape: discWithHole(), }); - drawOverWhite(renderer, effect); + await drawOverWhite(renderer, effect); const canvas = effect._cover.canvas; expect(renderer.cache.has(canvas)).toBe(true); effect.destroy(); expect(renderer.cache.has(canvas)).toBe(false); }); }); - - describe("WebGPU", () => { - let gpuApp; - let available = false; - beforeAll(async () => { - try { - gpuApp = new Application(SIZE, SIZE, { - parent: "screen", - renderer: video.WEBGPU, - consoleHeader: false, - }); - await gpuApp.init(); - available = gpuApp.renderer.type === "WebGPU"; - } catch { - available = false; - } - }); - afterAll(() => { - gpuApp?.destroy(); - }); - drawingChecks((ctx) => { - if (!available) { - ctx.skip("WebGPU not available in this environment"); - } - return gpuApp.renderer; - }); - }); }); describe("MaskEffect: what a shape and a center resolve to", () => { diff --git a/packages/melonjs/tests/sprite-frame-clamp.spec.js b/packages/melonjs/tests/sprite-frame-clamp.spec.js index 49ad1afbd8..653912a0c0 100644 --- a/packages/melonjs/tests/sprite-frame-clamp.spec.js +++ b/packages/melonjs/tests/sprite-frame-clamp.spec.js @@ -44,7 +44,7 @@ import WebGPULitQuadBatcher from "../src/video/webgpu/batchers/lit_quad_batcher. import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; import WebGPUUniformRing from "../src/video/webgpu/buffer/uniformring.js"; import { FRAME_UNIFORM_SIZE } from "../src/video/webgpu/pipeline/bindgroups.js"; -import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; +import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/tmxlayer.js"; import quadWGSL from "../src/video/webgpu/shaders/quad.wgsl"; import quadLitWGSL from "../src/video/webgpu/shaders/quad-lit.wgsl"; import WebGPURenderer from "../src/video/webgpu/webgpu_renderer.js"; @@ -1191,7 +1191,7 @@ describe("sprites keep to their frame (WebGPU, mock renderer)", () => { renderer.setBatcher = () => { return batcher; }; - const tmx = new WebGPUTMXRenderer(renderer); + const tmx = new WebGPUTMXRenderer(renderer, "orthogonal"); tmx.draw( { cols: 4, diff --git a/packages/melonjs/tests/texture-mipmap.spec.js b/packages/melonjs/tests/texture-mipmap.spec.js index 9e11496a88..3998929914 100644 --- a/packages/melonjs/tests/texture-mipmap.spec.js +++ b/packages/melonjs/tests/texture-mipmap.spec.js @@ -22,14 +22,18 @@ import Renderer from "../src/video/renderer.js"; import { createAtlas, TextureAtlas } from "../src/video/texture/atlas.js"; import WebGPULitQuadBatcher from "../src/video/webgpu/batchers/lit_quad_batcher.js"; import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; -import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; -import tmxlayerWGSL from "../src/video/webgpu/shaders/tmxlayer.wgsl"; +import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/tmxlayer.js"; +import tmxlayerCommonWGSL from "../src/video/webgpu/shaders/tmxlayer-common.wgsl"; +import tmxlayerOrthogonalWGSL from "../src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl"; import { getWebGLRenderer, releaseWebGLRenderer, } from "./helpers/webgl-context.js"; import { createMockWebGPURenderer } from "./helpers/webgpu-mock-renderer.js"; +// the orthogonal tile shader's module, as its renderer puts it together +const tmxlayerWGSL = `${tmxlayerCommonWGSL}\n${tmxlayerOrthogonalWGSL}`; + const SIZE = 64; // a checkerboard of single texels, `size` square, at (x, y) of `ctx` @@ -552,7 +556,7 @@ describe("linear-mipmap texture filter (WebGPU, mock renderer)", () => { renderer.setBatcher = () => { return batcher; }; - const tmx = new WebGPUTMXRenderer(renderer); + const tmx = new WebGPUTMXRenderer(renderer, "orthogonal"); tmx.draw(layer(atlas()), { pos: { x: 0, y: 0 }, width: 64, @@ -582,8 +586,10 @@ describe("linear-mipmap texture filter (WebGPU, mock renderer)", () => { expect(code).toContain( "return texel * tmx.uOpacity * vec4f(tmx.uTint.rgb, 1.0);", ); - expect(code.match(/layerColor\(result\.color\)/g)).toHaveLength(2); - expect(code).not.toMatch(/result\.color\.a \* tmx\.uOpacity/); + // every color the fragment stage returns goes through it: the + // single cell of the fast path, and what the cells add up to + expect(code.match(/return layerColor\(/g)).toHaveLength(2); + expect(code).not.toMatch(/\.a \* tmx\.uOpacity/); }); it("keeps each sample half a texel inside its tile, mipmaps or not", () => { @@ -594,7 +600,7 @@ describe("linear-mipmap texture filter (WebGPU, mock renderer)", () => { "let inset = min(vec2f(0.5 * exp2(lod * tmx.uMipmaps)), tmx.uTileSize * 0.5);", ); expect(code).toContain( - "texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + tmx.uTileSize - inset);", + "texelPx = clamp(texelPx, tileOriginPx + fp.inset, tileOriginPx + tmx.uTileSize - fp.inset);", ); }); }); diff --git a/packages/melonjs/tests/tmxlayer-edge-cases.spec.js b/packages/melonjs/tests/tmxlayer-edge-cases.spec.js new file mode 100644 index 0000000000..70835be045 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-edge-cases.spec.js @@ -0,0 +1,8 @@ +/** + * Tile layers at their edges, on WebGL: see + * `helpers/tile-layer-edge-cases.js`. + */ +import { video } from "../src/index.js"; +import { edgeCases } from "./helpers/tile-layer-edge-cases.js"; + +edgeCases(video.WEBGL, "WebGL"); diff --git a/packages/melonjs/tests/tmxlayer-offset.spec.js b/packages/melonjs/tests/tmxlayer-offset.spec.js new file mode 100644 index 0000000000..aba505bccf --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-offset.spec.js @@ -0,0 +1,427 @@ +/** + * Where a tile layer is: moved by its offset in Tiled and by its own `pos`, + * and in view as far as its tiles reach, which is past its grid when they + * are larger than a cell or moved by their tileset's `tileoffset`. + * + * Checked here on the Canvas renderer, tile by tile and baked into one + * image, against the picture painted with no engine (see + * `helpers/tile-layer-compare.js`), and for what `getTile` answers. The GPU + * renderers are held to the same in `helpers/tile-layer-isometric-scenarios.js`. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { video } from "../src/index.js"; +import { + diff, + drawIn, + drawn, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./helpers/tile-layer-compare.js"; + +const WIDTH = 320; +const HEIGHT = 240; + +const filled = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return (c * 3 + r) % 7 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }); +}; + +const orthogonal = (options = {}) => { + return { + orientation: "orthogonal", + cols: 10, + rows: 8, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: options.tall ?? 32 })], + layers: [{ data: filled(10, 8), ...options.layer }], + }; +}; + +const isometric = (options = {}) => { + const th = options.tall ?? 64; + return { + orientation: "isometric", + cols: 8, + rows: 6, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th, + opaque: (x, y) => { + return inBlock(x, y, 64, th, 64, 32); + }, + }), + ], + layers: [{ data: filled(8, 6), ...options.layer }], + }; +}; + +describe("where a tile layer is (Canvas renderer)", () => { + let app; + beforeAll(async () => { + app = await makeApp(video.CANVAS, "CANVAS", WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + /** + * Draw a scene tile by tile and baked, from each view, and expect the + * reference picture. + * @returns {Promise} how many pixels were drawn, in all + */ + const asTiled = async (scene, views) => { + let total = 0; + for (const mode of ["perTile", "prerender"]) { + const layers = show(app, makeMap(scene), mode); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, mode); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe(0); + total += drawn(frame); + } + } + return total; + }; + + const VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + ]; + + describe("moved by its offset in Tiled", () => { + for (const [x, y] of [ + [37, -21], + [-130, 75], + ]) { + it(`an orthogonal layer, by (${x}, ${y})`, async () => { + const scene = orthogonal({ + tall: 64, + layer: { offsetx: x, offsety: y }, + }); + expect(await asTiled(scene, VIEWS)).toBeGreaterThan(0); + }); + + it(`an isometric layer, by (${x}, ${y})`, async () => { + const scene = isometric({ + tall: 128, + layer: { offsetx: x, offsety: y }, + }); + expect(await asTiled(scene, VIEWS)).toBeGreaterThan(0); + }); + } + + for (const [orientation, extra] of [ + ["staggered", { staggeraxis: "y", staggerindex: "odd" }], + [ + "hexagonal", + { staggeraxis: "x", staggerindex: "even", hexsidelength: 16 }, + ], + ]) { + it(`a ${orientation} layer: the same picture, moved`, async () => { + // no reference is painted for these maps: the layer moved by + // (37, -21) is the layer where it was, seen from that much + // the other way + const scene = (layer) => { + return { + orientation, + cols: 8, + rows: 8, + tw: 64, + th: 32, + tilesets: [tileset], + layers: [{ data: filled(8, 8), ...layer }], + extra, + }; + }; + const tileset = makeTileset({ + tw: 64, + th: 48, + opaque: (x, y) => { + return inBlock(x, y, 64, 48, 64, 32); + }, + }); + for (const mode of ["perTile", "prerender"]) { + const plain = show(app, makeMap(scene({})), mode); + const frames = []; + for (const [x, y] of VIEWS) { + app.viewport.moveTo(x - 37, y + 21); + frames.push((await drawIn(app, plain, mode)).frame); + } + const moved = show( + app, + makeMap(scene({ offsetx: 37, offsety: -21 })), + mode, + ); + for (const [i, [x, y]] of VIEWS.entries()) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, moved, mode); + const d = diff(frames[i], frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe( + 0, + ); + expect(drawn(frame)).toBeGreaterThan(0); + } + } + }); + } + + it("the layer's pos is its offset, and its bounds move with it", () => { + const [layer] = show( + app, + makeMap(orthogonal({ layer: { offsetx: 37, offsety: -21 } })), + ); + expect([layer.pos.x, layer.pos.y]).toEqual([37, -21]); + expect([layer.width, layer.height]).toEqual([320, 256]); + const bounds = layer.getBounds(); + expect([bounds.left, bounds.top]).toEqual([37, -21]); + expect([bounds.right, bounds.bottom]).toEqual([357, 235]); + // no offset: where it always was + const [plain] = show(app, makeMap(orthogonal())); + expect([plain.pos.x, plain.pos.y]).toEqual([0, 0]); + }); + + it("getTile answers for where the tiles are drawn", () => { + const [layer] = show( + app, + makeMap(orthogonal({ layer: { offsetx: 40, offsety: -24 } })), + ); + // the cell (2, 3) is drawn from (64 + 40, 96 - 24) + const tile = layer.getTile(64 + 40 + 5, 96 - 24 + 5); + expect([tile.col, tile.row]).toEqual([2, 3]); + expect(tile.tileId).toBe(layer.cellAt(2, 3).tileId); + // its last pixel, and the first of the next cell + expect(layer.getTile(95 + 40, 127 - 24).col).toBe(2); + expect(layer.getTile(96 + 40, 127 - 24).col).toBe(3); + // left of where the layer now starts: no tile, where there was + // one before the layer was moved + expect(layer.getTile(5, 40)).toBeNull(); + const first = layer.getTile(32 + 40 + 5, -20); + expect([first.col, first.row]).toEqual([1, 0]); + + const [iso] = show( + app, + makeMap(isometric({ layer: { offsetx: -30, offsety: 50 } })), + ); + // the middle of the diamond of cell (3, 2): the map's top + // corner is at x = rows × half a tile + const originX = 6 * 32; + const middle = iso.getTile( + (3 - 2) * 32 + originX - 30, + (3 + 2 + 1) * 16 + 50, + ); + expect([middle.col, middle.row]).toEqual([3, 2]); + }); + }); + + describe("getTile, on every kind of map, with the layer moved", () => { + const grids = [ + ["orthogonal", 32, 32, {}], + ["isometric", 64, 32, {}], + ["staggered", 64, 32, { staggeraxis: "y", staggerindex: "odd" }], + ["staggered", 64, 32, { staggeraxis: "x", staggerindex: "even" }], + [ + "hexagonal", + 28, + 24, + { staggeraxis: "y", staggerindex: "odd", hexsidelength: 12 }, + ], + [ + "hexagonal", + 28, + 24, + { staggeraxis: "x", staggerindex: "even", hexsidelength: 11 }, + ], + ]; + for (const [orientation, tw, th, extra] of grids) { + it(`${orientation}${extra.staggeraxis ? `, axis ${extra.staggeraxis}` : ""}: the middle of every cell is that cell`, () => { + const cols = 7; + const rows = 6; + const scene = { + orientation, + cols, + rows, + tw, + th, + tilesets: [makeTileset({ tw, th })], + layers: [{ data: filled(cols, rows), offsetx: 37, offsety: -21 }], + extra, + }; + const [layer] = show(app, makeMap(scene)); + const map = layer.getRenderer(); + let found = 0; + for (const [dx, dy] of [ + [0, 0], + [55, -140], + ]) { + layer.pos.x += dx; + layer.pos.y += dy; + const movedX = 37 + dx; + const movedY = -21 + dy; + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { + // the middle of the cell, where the map's renderer + // puts it, moved as the layer is + const at = + orientation === "isometric" + ? map.tileToPixelCoords(col + 0.5, row + 0.5) + : map.tileToPixelCoords(col, row); + if (orientation !== "isometric") { + at.x += map.tilewidth / 2; + at.y += map.tileheight / 2; + } + const tile = layer.getTile(at.x + movedX, at.y + movedY); + const gid = scene.layers[0].data[row * cols + col]; + if (gid === 0) { + expect(tile, `empty cell (${col}, ${row})`).toBeNull(); + } else { + expect( + [tile?.col, tile?.row], + `cell (${col}, ${row}), layer moved by (${movedX}, ${movedY})`, + ).toEqual([col, row]); + expect(tile.tileId).toBe(gid); + found++; + } + } + } + } + expect(found).toBeGreaterThan(40); + // and far from the layer, nothing + expect(layer.getTile(-5000, -5000)).toBeNull(); + }); + } + }); + + describe("moved in the game, by its pos", () => { + for (const make of [orthogonal, isometric]) { + it(`${make.name}: drawn, and found by getTile, where it was moved to`, async () => { + const scene = make({ tall: 64 }); + const layers = show(app, makeMap(scene), "perTile"); + const layer = layers[0]; + const before = layer.getTile(150, 100); + expect(before).not.toBeNull(); + for (const [dx, dy] of [ + [50, -30], + [-170, 90], + [0, 0], + ]) { + layer.pos.x = dx; + layer.pos.y = dy; + scene.layers[0].offsetx = dx; + scene.layers[0].offsety = dy; + for (const [x, y] of VIEWS) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "perTile"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect( + d.different, + `moved by (${dx}, ${dy}), view (${x}, ${y}): ${d.first}`, + ).toBe(0); + } + // the same cell, where it is now + const now = layer.getTile(150 + dx, 100 + dy); + expect([now.col, now.row]).toEqual([before.col, before.row]); + } + }); + } + }); + + describe("in view as far as its tiles reach", () => { + it("isometric: tall tiles, above the map's top corner", async () => { + const scene = isometric({ tall: 128 }); + scene.layers[0].data = fill(8, 6, (c, r) => { + return 1 + ((c + r * 2) % 8); + }); + // the grid starts at y = 0: these views end above it + const views = [ + [40, -HEIGHT - 10], + [100, -HEIGHT - 60], + ]; + expect(await asTiled(scene, views)).toBeGreaterThan(0); + }); + + it("orthogonal: tall tiles above the first row, wide ones right of the last column", async () => { + const tall = orthogonal({ tall: 128 }); + expect( + await asTiled(tall, [ + [0, -HEIGHT - 10], + [-200, -HEIGHT - 80], + ]), + ).toBeGreaterThan(0); + const wide = orthogonal(); + wide.tilesets = [makeTileset({ tw: 96, th: 32 })]; + expect( + await asTiled(wide, [ + [325, 0], + [380, -100], + ]), + ).toBeGreaterThan(0); + }); + + it("tiles moved off the grid by their tileset's tileoffset", async () => { + const scene = orthogonal(); + scene.tilesets = [ + makeTileset({ tw: 32, th: 32, extra: { tileoffset: { x: 0, y: 90 } } }), + ]; + // the grid ends at y = 256 + expect( + await asTiled(scene, [ + [0, 260], + [-40, 300], + ]), + ).toBeGreaterThan(0); + }); + + it("its bounds stay the grid's: the reach is for what is in view alone", () => { + const [layer] = show(app, makeMap(orthogonal({ tall: 128 }))); + expect([layer.width, layer.height]).toEqual([320, 256]); + const bounds = layer.getBounds(); + expect([bounds.left, bounds.top, bounds.right, bounds.bottom]).toEqual([ + 0, 0, 320, 256, + ]); + // 128 pixel tiles on a 32 pixel grid stand 96 above their cell + const reach = layer.getDrawBounds(); + expect([reach.left, reach.top, reach.right, reach.bottom]).toEqual([ + 0, -96, 320, 256, + ]); + // and getTile has nothing above the grid: no cell is there + expect(layer.getTile(10, -50)).toBeNull(); + // moved, both move with it + layer.pos.x = 40; + layer.pos.y = -10; + expect([layer.getBounds().left, layer.getBounds().top]).toEqual([ + 40, -10, + ]); + expect([layer.getDrawBounds().left, layer.getDrawBounds().top]).toEqual([ + 40, -106, + ]); + // tiles the size of the grid: the grid, no more + const plain = show(app, makeMap(orthogonal()))[0].getDrawBounds(); + expect([plain.left, plain.top, plain.right, plain.bottom]).toEqual([ + 0, 0, 320, 256, + ]); + }); + + it("and far from all of it, the layer is not drawn at all", async () => { + const scene = orthogonal({ tall: 128 }); + const layers = show(app, makeMap(scene), "perTile"); + app.viewport.moveTo(0, -HEIGHT - 200); + const far = await drawIn(app, layers, "perTile"); + expect(far.cpuDraws).toBe(0); + expect(drawn(far.frame)).toBe(0); + }); + }); +}); diff --git a/packages/melonjs/tests/tmxlayer-placement-webgl.spec.js b/packages/melonjs/tests/tmxlayer-placement-webgl.spec.js new file mode 100644 index 0000000000..0b47582a68 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-placement-webgl.spec.js @@ -0,0 +1,8 @@ +/** + * A tile layer drawn where its ancestors put it, on WebGL, with the GPU + * path: see `helpers/tile-layer-placement-checks.js`. + */ +import { video } from "../src/index.js"; +import { placementChecks } from "./helpers/tile-layer-placement-checks.js"; + +placementChecks(video.WEBGL, "WebGL", ["perTile", "shader", "prerender"]); diff --git a/packages/melonjs/tests/tmxlayer-placement.spec.js b/packages/melonjs/tests/tmxlayer-placement.spec.js new file mode 100644 index 0000000000..d073279d17 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-placement.spec.js @@ -0,0 +1,8 @@ +/** + * A tile layer drawn where its ancestors put it, on the Canvas renderer: + * see `helpers/tile-layer-placement-checks.js`. + */ +import { video } from "../src/index.js"; +import { placementChecks } from "./helpers/tile-layer-placement-checks.js"; + +placementChecks(video.CANVAS, "CANVAS", ["perTile", "prerender"]); diff --git a/packages/melonjs/tests/tmxlayer-shader-isometric.spec.js b/packages/melonjs/tests/tmxlayer-shader-isometric.spec.js new file mode 100644 index 0000000000..e8363cc0f4 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-shader-isometric.spec.js @@ -0,0 +1,8 @@ +/** + * Isometric tile layers on the GPU, on WebGL: see + * `helpers/tile-layer-isometric-scenarios.js`. + */ +import { video } from "../src/index.js"; +import { isometricScenarios } from "./helpers/tile-layer-isometric-scenarios.js"; + +isometricScenarios(video.WEBGL, "WebGL"); diff --git a/packages/melonjs/tests/tmxlayer-shader-staggered.spec.js b/packages/melonjs/tests/tmxlayer-shader-staggered.spec.js new file mode 100644 index 0000000000..625d924051 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-shader-staggered.spec.js @@ -0,0 +1,8 @@ +/** + * Staggered and hexagonal tile layers on the GPU, on WebGL: see + * `helpers/tile-layer-staggered-scenarios.js`. + */ +import { video } from "../src/index.js"; +import { staggeredScenarios } from "./helpers/tile-layer-staggered-scenarios.js"; + +staggeredScenarios(video.WEBGL, "WebGL"); diff --git a/packages/melonjs/tests/tmxlayer-shader.spec.js b/packages/melonjs/tests/tmxlayer-shader.spec.js index d1e3b61818..d12529101b 100644 --- a/packages/melonjs/tests/tmxlayer-shader.spec.js +++ b/packages/melonjs/tests/tmxlayer-shader.spec.js @@ -1,7 +1,7 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { boot } from "../src/index.js"; import { BufferTextureResource } from "../src/video/texture/resource.js"; -import OrthogonalTMXLayerGPURenderer from "../src/video/webgl/renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "../src/video/webgl/renderers/tmxlayer/tmxlayer.js"; import { getWebGLRenderer, releaseWebGLRenderer, @@ -44,7 +44,7 @@ describe("TMXLayer shader path", () => { // `_getTMXGPURendererFor` is the lazy factory on WebGLRenderer const r1 = renderer._getTMXGPURendererFor("orthogonal"); const r2 = renderer._getTMXGPURendererFor("orthogonal"); - expect(r1).toBeInstanceOf(OrthogonalTMXLayerGPURenderer); + expect(r1).toBeInstanceOf(TMXLayerGPURenderer); expect(r2).toBe(r1); }); diff --git a/packages/melonjs/tests/tmxlayer-staggered.spec.js b/packages/melonjs/tests/tmxlayer-staggered.spec.js new file mode 100644 index 0000000000..47cc976e3e --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-staggered.spec.js @@ -0,0 +1,53 @@ +/** + * Staggered and hexagonal maps drawn tile by tile, and baked into one image, + * on the Canvas renderer: held to the picture Tiled draws (a tile stands on + * its cell and follows its tileset's `tileoffset`, as on every other map). + * The GPU renderers are held to the same in + * `helpers/tile-layer-staggered-scenarios.js`. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { video } from "../src/index.js"; +import { + diff, + drawIn, + drawn, + makeApp, + makeMap, + reference, + show, +} from "./helpers/tile-layer-compare.js"; +import { + HEIGHT, + STAGGERED_SCENES, + STAGGERED_VIEWS, + WIDTH, +} from "./helpers/tile-layer-staggered-scenarios.js"; + +describe("staggered and hexagonal maps, as Tiled draws them (Canvas renderer)", () => { + let app; + beforeAll(async () => { + app = await makeApp(video.CANVAS, "CANVAS", WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + for (const { label, scene: build } of STAGGERED_SCENES) { + it(label, async () => { + const scene = build(); + let total = 0; + for (const mode of ["perTile", "prerender"]) { + const layers = show(app, makeMap(scene), mode); + for (const [x, y] of STAGGERED_VIEWS) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, mode); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe(0); + total += drawn(frame); + } + } + expect(total).toBeGreaterThan(0); + }); + } +}); diff --git a/packages/melonjs/tests/tmxlayer_bench.spec.js b/packages/melonjs/tests/tmxlayer_bench.spec.js new file mode 100644 index 0000000000..bd8f38f9ec --- /dev/null +++ b/packages/melonjs/tests/tmxlayer_bench.spec.js @@ -0,0 +1,201 @@ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { video } from "../src/index.js"; +import TMXTileset from "../src/level/tiled/TMXTileset.js"; +import Renderer from "../src/video/renderer.js"; +import { + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + show, +} from "./helpers/tile-layer-compare.js"; + +/** + * Benchmark of the two ways a tile layer is drawn on WebGL: tile by tile + * (`renderMode` "perTile", the CPU renderer: a quad per tile in view, each + * found and placed in JavaScript every frame) against the GPU path + * ("shader": one quad per layer, the tiles found by the fragment shader), + * on a map of medium size, orthogonal and isometric (#1897). + * + * Read the times as **CPU-side** cost, the main thread's share of a frame: + * the run happens under a software rasterizer, whose own time is no measure + * of a graphics card's, and is left out (the frames are timed up to the + * flush, and the rasterizer is let catch up between runs). What the GPU + * path takes off the main thread is the walk over the cells in view and a + * quad for each: its cost does not grow with the number of tiles shown, + * which is what the wider views show. + * + * The canvas is a quarter of the view's size each way, and the camera + * zoomed out to match: as many tiles are in view, and walked, as in the + * view named, with a sixteenth of the pixels for the software rasterizer. + */ + +const SCALE = 4; +// the views measured: what a camera shows, in pixels of the map +const VIEWS = [ + [1280, 720], + [2560, 1440], + [3840, 2160], +]; +const SIZE = 100; // cells a side +const FRAMES = 20; +const RUNS = 3; + +/** most cells filled, a few holes, as a drawn map has */ +const cells = () => { + return fill(SIZE, SIZE, (c, r) => { + return (c * 7 + r * 3) % 23 === 0 ? 0 : 1 + ((c * 5 + r * 11) % 8); + }); +}; + +const MAPS = [ + { + label: "orthogonal, 32 × 32", + scene: () => { + return { + orientation: "orthogonal", + cols: SIZE, + rows: SIZE, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: cells() }], + }; + }, + // the middle of the map + center: [(SIZE * 32) / 2, (SIZE * 32) / 2], + }, + { + label: "isometric, 64 × 64 on a 64 × 32 grid", + scene: () => { + return { + orientation: "isometric", + cols: SIZE, + rows: SIZE, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th: 64, + opaque: (x, y) => { + return inBlock(x, y, 64, 64, 64, 32); + }, + }), + ], + layers: [{ data: cells() }], + }; + }, + center: [(SIZE * 64) / 2, (SIZE * 32) / 2], + }, +]; + +describe("tile layer benchmark: tile by tile against the GPU path", () => { + let app; + beforeAll(async () => { + app = await makeApp( + video.WEBGL, + "WebGL", + VIEWS[0][0] / SCALE, + VIEWS[0][1] / SCALE, + ); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + it(`${SIZE} × ${SIZE} maps, three sizes of view`, (ctx) => { + if (app === null || app === undefined) { + // a bare return reports as a PASS, so a benchmark that never + // ran would look identical to one that did + ctx.skip("WebGL not available: nothing to measure"); + return; + } + const gl = app.renderer.gl; + const pixel = new Uint8Array(4); + const frame = () => { + app.world.update(0); + app.viewport.update(0); + app.repaint(); + app.draw(); + app.renderer.flush(); + }; + // let the rasterizer finish what it was sent, outside the timings + const settle = () => { + gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel); + }; + /** the median time of a frame, in ms */ + const time = () => { + for (let i = 0; i < 5; i++) { + frame(); + } + settle(); + const runs = []; + for (let run = 0; run < RUNS; run++) { + const start = performance.now(); + for (let i = 0; i < FRAMES; i++) { + frame(); + } + runs.push((performance.now() - start) / FRAMES); + settle(); + } + runs.sort((a, b) => { + return a - b; + }); + return runs[Math.floor(RUNS / 2)]; + }; + + const rows = []; + for (const { label, scene, center } of MAPS) { + const layers = show(app, makeMap(scene())); + expect(layers[0].renderMode).toBe("shader"); + for (const [width, height] of VIEWS) { + app.viewport.zoom = VIEWS[0][0] / SCALE / width; + app.viewport.moveTo(center[0] - width / 2, center[1] - height / 2); + + layers[0].renderMode = "perTile"; + const tiles = vi.spyOn(TMXTileset.prototype, "drawTileRaw"); + frame(); + const inView = tiles.mock.calls.length; + tiles.mockRestore(); + const perTile = time(); + + layers[0].renderMode = "shader"; + const cpu = vi.spyOn(Renderer.prototype, "drawTileLayer"); + frame(); + // the GPU path draws alone + expect(cpu).not.toHaveBeenCalled(); + cpu.mockRestore(); + const shader = time(); + + expect(inView).toBeGreaterThan(0); + rows.push({ + label, + view: `${width} × ${height}`, + inView, + perTile, + shader, + }); + } + app.viewport.zoom = 1; + } + + const line = "═".repeat(86); + console.log(`╔${line}╗`); + console.log( + `║ ${`TILE LAYER BENCHMARK — ${SIZE} × ${SIZE} map, CPU ms per frame`.padEnd(85)}║`, + ); + console.log(`╠${line}╣`); + console.log( + `║ ${"map".padEnd(36)}${"view".padStart(13)}${"tiles".padStart(8)}${"perTile".padStart(10)}${"shader".padStart(9)}${"ratio".padStart(8)} ║`, + ); + for (const row of rows) { + console.log( + `║ ${row.label.padEnd(36)}${row.view.padStart(13)}${String(row.inView).padStart(8)}${row.perTile.toFixed(3).padStart(10)}${row.shader.toFixed(3).padStart(9)}${`${(row.perTile / row.shader).toFixed(1)}×`.padStart(8)} ║`, + ); + } + console.log(`╚${line}╝`); + }); +}); diff --git a/packages/melonjs/tests/webgpu_tmxlayer.spec.js b/packages/melonjs/tests/webgpu_tmxlayer.spec.js index f5ebdb8117..e8745aee51 100644 --- a/packages/melonjs/tests/webgpu_tmxlayer.spec.js +++ b/packages/melonjs/tests/webgpu_tmxlayer.spec.js @@ -1,7 +1,7 @@ import "./helpers/webgpu-globals.js"; import { beforeEach, describe, expect, it } from "vitest"; import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; -import OrthogonalTMXLayerGPURenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "../src/video/webgpu/renderers/tmxlayer/tmxlayer.js"; import { createMockWebGPURenderer } from "./helpers/webgpu-mock-renderer.js"; /** @@ -58,13 +58,13 @@ describe("WebGPU orthogonal TMX layer renderer (mock)", () => { renderer.currentBatcher ??= new WebGPUQuadBatcher(renderer); return renderer.currentBatcher; }; - tmx = new OrthogonalTMXLayerGPURenderer(renderer); + tmx = new TMXLayerGPURenderer(renderer, "orthogonal"); }); it("registers the shader family once with the quad vertex layout", () => { expect(tmx.key).toMatch(/^effect:/); // a second instance (device-loss rebuild) reuses the module text - const again = new OrthogonalTMXLayerGPURenderer(renderer); + const again = new TMXLayerGPURenderer(renderer, "orthogonal"); expect(again.key).toBe(tmx.key); }); @@ -185,3 +185,204 @@ describe("WebGPU orthogonal TMX layer renderer (mock)", () => { expect(tmx.animLookups.size).toBe(0); }); }); + +/** + * The isometric orientation on the same renderer (#1897): its own shader + * module, one pass with the layer's one tileset whatever the map has, and + * the two run lengths its shader walks in place of the orthogonal overflow. + * What it draws is compared with the CPU renderer, pixel by pixel, on a real + * device in `webgpu_tmxlayer_isometric.spec.js`. + */ +describe("WebGPU isometric TMX layer renderer (mock)", () => { + let renderer; + let tmx; + + const tileset = (firstgid, tilewidth, tileheight) => { + return { + isCollection: false, + image: { width: 256, height: 256 }, + tilewidth, + tileheight, + margin: 0, + spacing: 0, + tileoffset: { x: 0, y: 0 }, + firstgid, + lastgid: firstgid + 15, + isAnimated: false, + animations: new Map(), + texture: { name: `iso-atlas-${firstgid}` }, + }; + }; + const floor = tileset(1, 64, 32); + const walls = tileset(17, 64, 128); + + const layerOf = (shaderTileset) => { + return { + orientation: "isometric", + cols: 8, + rows: 8, + tilewidth: 64, + tileheight: 32, + dataVersion: 1, + layerData: new Uint16Array(8 * 8 * 2), + getOpacity() { + return 1; + }, + getRenderer() { + return { originX: 8 * 32 }; + }, + tint: null, + tilesets: { tilesets: [floor, walls] }, + _shaderTileset: shaderTileset, + }; + }; + + const RECT = { pos: { x: 100, y: 40 }, width: 320, height: 240 }; + + beforeEach(() => { + renderer = createMockWebGPURenderer(); + renderer.setBatcher = () => { + renderer.currentBatcher ??= new WebGPUQuadBatcher(renderer); + return renderer.currentBatcher; + }; + tmx = new TMXLayerGPURenderer(renderer, "isometric"); + }); + + it("has a shader module of its own", () => { + const orthogonal = new TMXLayerGPURenderer(renderer, "orthogonal"); + expect(tmx.key).toMatch(/^effect:/); + expect(tmx.key).not.toBe(orthogonal.key); + }); + + it("draws one quad, with the layer's tileset alone", () => { + tmx.draw(layerOf(walls), RECT); + expect(renderer.calls.drawIndexed).toEqual([6]); + // the tiles' size, and how many cells' images reach a pixel: as + // many across as the image is wide in half grid widths, as many + // down as it is tall in half grid heights + const uniforms = tmx.uniformScratch; + expect([uniforms[4], uniforms[5]]).toEqual([64, 128]); + expect([uniforms[6], uniforms[7]]).toEqual([2, 8]); + }); + + it("draws nothing for a layer with no tile yet", () => { + tmx.draw(layerOf(undefined), RECT); + expect(renderer.calls.drawIndexed).toEqual([]); + }); + + it("draws nothing where the view shows none of the layer", () => { + tmx.draw(layerOf(floor), { + pos: { x: 5000, y: 5000 }, + width: 320, + height: 240, + }); + expect(renderer.calls.drawIndexed).toEqual([]); + }); +}); + +/** + * Staggered and hexagonal maps on the same renderer: one shader module for + * both, with the map's column width, row height, stagger axis and index in + * what was the uniform block's padding. + */ +describe("WebGPU staggered and hexagonal TMX layer renderer (mock)", () => { + let renderer; + + const tileset = { + isCollection: false, + image: { width: 256, height: 256 }, + tilewidth: 28, + tileheight: 48, + margin: 0, + spacing: 0, + tileoffset: { x: 0, y: 0 }, + firstgid: 1, + lastgid: 16, + isAnimated: false, + animations: new Map(), + texture: { name: "hex-atlas" }, + }; + + const layerOf = (orientation, map) => { + return { + orientation, + cols: 8, + rows: 8, + tilewidth: 28, + tileheight: 24, + dataVersion: 1, + layerData: new Uint16Array(8 * 8 * 2), + getOpacity() { + return 1; + }, + getRenderer() { + return map; + }, + tint: null, + tilesets: { tilesets: [tileset] }, + _shaderTileset: tileset, + }; + }; + + const RECT = { pos: { x: 10, y: 20 }, width: 320, height: 240 }; + + beforeEach(() => { + renderer = createMockWebGPURenderer(); + renderer.setBatcher = () => { + renderer.currentBatcher ??= new WebGPUQuadBatcher(renderer); + return renderer.currentBatcher; + }; + }); + + it("one shader module for both, apart from the other orientations'", () => { + const staggered = new TMXLayerGPURenderer(renderer, "staggered"); + const hexagonal = new TMXLayerGPURenderer(renderer, "hexagonal"); + const isometric = new TMXLayerGPURenderer(renderer, "isometric"); + expect(hexagonal.key).toBe(staggered.key); + expect(staggered.key).not.toBe(isometric.key); + }); + + it("hands the shader the map's lines: their spacing, axis and index", () => { + const tmx = new TMXLayerGPURenderer(renderer, "hexagonal"); + // a hexagon side of 12 along x: columns 20 apart, rows of 12 + tmx.draw( + layerOf("hexagonal", { + columnwidth: 20, + rowheight: 12, + staggerX: true, + staggerEven: true, + }), + RECT, + ); + expect(renderer.calls.drawIndexed).toEqual([6]); + const uniforms = tmx.uniformScratch; + // axis x (1) and an even index (2) + expect(uniforms[29]).toBe(3); + expect([uniforms[30], uniforms[31]]).toEqual([20, 12]); + // 28 pixels wide in spacings of 40, 48 tall in rows of 12 + expect([uniforms[6], uniforms[7]]).toEqual([1, 4]); + + const other = new TMXLayerGPURenderer(renderer, "staggered"); + other.draw( + layerOf("staggered", { + columnwidth: 14, + rowheight: 12, + staggerX: false, + staggerEven: false, + }), + RECT, + ); + expect(other.uniformScratch[29]).toBe(0); + expect([other.uniformScratch[30], other.uniformScratch[31]]).toEqual([ + 14, 12, + ]); + }); + + it("leaves them at 0 for the other orientations", () => { + const tmx = new TMXLayerGPURenderer(renderer, "isometric"); + const layer = layerOf("isometric", { originX: 8 * 14 }); + tmx.draw(layer, RECT); + expect(renderer.calls.drawIndexed).toEqual([6]); + expect([...tmx.uniformScratch.slice(29, 32)]).toEqual([0, 0, 0]); + }); +}); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js new file mode 100644 index 0000000000..4f5f3b8e28 --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js @@ -0,0 +1,9 @@ +/** + * Tile layers at their edges, on WebGPU: see + * `helpers/tile-layer-edge-cases.js`. Skipped where the browser has no + * WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { edgeCases } from "./helpers/tile-layer-edge-cases.js"; + +edgeCases(video.WEBGPU, "WebGPU"); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js new file mode 100644 index 0000000000..c4ae0bf009 --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js @@ -0,0 +1,9 @@ +/** + * Isometric tile layers on the GPU, on WebGPU: see + * `helpers/tile-layer-isometric-scenarios.js`. Skipped, visibly, where the + * browser has no WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { isometricScenarios } from "./helpers/tile-layer-isometric-scenarios.js"; + +isometricScenarios(video.WEBGPU, "WebGPU"); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js new file mode 100644 index 0000000000..6ebc8fba77 --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js @@ -0,0 +1,9 @@ +/** + * A tile layer drawn where its ancestors put it, on WebGPU: see + * `helpers/tile-layer-placement-checks.js`. Skipped where the browser has + * no WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { placementChecks } from "./helpers/tile-layer-placement-checks.js"; + +placementChecks(video.WEBGPU, "WebGPU", ["perTile", "shader", "prerender"]); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js new file mode 100644 index 0000000000..4fd65a742f --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js @@ -0,0 +1,9 @@ +/** + * Staggered and hexagonal tile layers on the GPU, on WebGPU: see + * `helpers/tile-layer-staggered-scenarios.js`. Skipped, visibly, where the + * browser has no WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { staggeredScenarios } from "./helpers/tile-layer-staggered-scenarios.js"; + +staggeredScenarios(video.WEBGPU, "WebGPU"); diff --git a/packages/pathfinding-plugin/CHANGELOG.md b/packages/pathfinding-plugin/CHANGELOG.md index 21c213f86d..6de3ce7d2b 100644 --- a/packages/pathfinding-plugin/CHANGELOG.md +++ b/packages/pathfinding-plugin/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [1.1.1] - _unreleased_ + +### Fixed +- `Grid.fromTileLayer`: the grid stands where the layer's tiles are drawn when the layer has an offset in Tiled or was moved by its `pos`, which melonJS 20.17 follows when it draws the layer; the grid stayed at the map's origin + ## [1.1.0] - _2026-11-15_ ### Added diff --git a/packages/pathfinding-plugin/package.json b/packages/pathfinding-plugin/package.json index e6b114272d..9b894c5672 100644 --- a/packages/pathfinding-plugin/package.json +++ b/packages/pathfinding-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@melonjs/pathfinding-plugin", - "version": "1.1.0", + "version": "1.1.1", "description": "melonJS A* pathfinding plugin", "homepage": "https://www.npmjs.com/package/@melonjs/pathfinding-plugin", "type": "module", diff --git a/packages/pathfinding-plugin/src/grid.ts b/packages/pathfinding-plugin/src/grid.ts index b9f9deb7c6..fa9c4472c8 100644 --- a/packages/pathfinding-plugin/src/grid.ts +++ b/packages/pathfinding-plugin/src/grid.ts @@ -595,6 +595,14 @@ export class Grid { corner.x += at.x - layer.pos.x; corner.y += at.y - layer.pos.y; } + // and where the layer was moved to in its map, by its offset in + // Tiled or by its `pos` (an engine that draws it there says where it + // stands unmoved; an older one drew it unmoved, whatever its `pos`) + const unmoved = (layer as { _mapPos?: { x: number; y: number } })._mapPos; + if (unmoved !== undefined) { + corner.x += layer.pos.x - unmoved.x; + corner.y += layer.pos.y - unmoved.y; + } if (isometric) { // the grid's corner is the box around its diamonds corner.x -= (layer.rows * layer.tilewidth) / 2; diff --git a/packages/pathfinding-plugin/tests/sources.spec.ts b/packages/pathfinding-plugin/tests/sources.spec.ts index 406eb64448..d293d76a50 100644 --- a/packages/pathfinding-plugin/tests/sources.spec.ts +++ b/packages/pathfinding-plugin/tests/sources.spec.ts @@ -230,6 +230,27 @@ describe("grids from a level", () => { grid.refresh(); expect(grid.isSolid(2, 1)).toBe(true); }); + + it("stands where the layer's tiles are drawn, when the layer was moved", () => { + // by its offset in Tiled, or as here by its `pos`: the tiles + // are drawn there, and the grid's cells are theirs + const layer = wallsLayer(); + const before = Grid.fromTileLayer(layer).getPointPosition(2, 1); + const { x, y } = layer.pos; + layer.pos.x = x + 24; + layer.pos.y = y - 10; + try { + const grid = Grid.fromTileLayer(layer); + const at = grid.getPointPosition(2, 1); + expect([at.x - before.x, at.y - before.y]).toEqual([24, -10]); + // the same cells, moved + expect(grid.isSolid(2, 0)).toBe(true); + expect(grid.isSolid(0, 0)).toBe(false); + } finally { + layer.pos.x = x; + layer.pos.y = y; + } + }); }); describe("Grid.fromBodies", () => {