@react-x11/components 0.7.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +295 -51
- package/dist/charts/node.d.ts +1 -1
- package/dist/charts/node.d.ts.map +1 -1
- package/dist/charts/node.js +6 -2
- package/dist/charts/node.js.map +1 -1
- package/dist/color-picker/ColorPicker.d.ts.map +1 -1
- package/dist/color-picker/ColorPicker.js +43 -33
- package/dist/color-picker/ColorPicker.js.map +1 -1
- package/dist/embed/client.d.ts +48 -3
- package/dist/embed/client.d.ts.map +1 -1
- package/dist/embed/client.js +79 -5
- package/dist/embed/client.js.map +1 -1
- package/dist/embed/index.d.ts +1 -1
- package/dist/embed/index.d.ts.map +1 -1
- package/dist/embed/index.js +1 -1
- package/dist/embed/index.js.map +1 -1
- package/dist/html/node.d.ts +3 -1
- package/dist/html/node.d.ts.map +1 -1
- package/dist/html/node.js +7 -5
- package/dist/html/node.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/{markdown → internal/markdown}/ast.d.ts +12 -0
- package/dist/internal/markdown/ast.d.ts.map +1 -0
- package/dist/internal/markdown/ast.js +22 -0
- package/dist/internal/markdown/ast.js.map +1 -0
- package/dist/internal/markdown/parse.d.ts.map +1 -0
- package/dist/{markdown → internal/markdown}/parse.js +112 -22
- package/dist/internal/markdown/parse.js.map +1 -0
- package/dist/internal/markdown/stringify.d.ts +4 -0
- package/dist/internal/markdown/stringify.d.ts.map +1 -0
- package/dist/internal/markdown/stringify.js +684 -0
- package/dist/internal/markdown/stringify.js.map +1 -0
- package/dist/internal/markdown/tags.d.ts.map +1 -0
- package/dist/internal/markdown/tags.js.map +1 -0
- package/dist/internal/scroll.d.ts +3 -2
- package/dist/internal/scroll.d.ts.map +1 -1
- package/dist/internal/scroll.js +33 -15
- package/dist/internal/scroll.js.map +1 -1
- package/dist/internal/units.d.ts +2 -0
- package/dist/internal/units.d.ts.map +1 -0
- package/dist/internal/units.js +35 -0
- package/dist/internal/units.js.map +1 -0
- package/dist/internal/window.d.ts.map +1 -1
- package/dist/internal/window.js +4 -2
- package/dist/internal/window.js.map +1 -1
- package/dist/maps/anchors.d.ts +98 -0
- package/dist/maps/anchors.d.ts.map +1 -0
- package/dist/maps/anchors.js +531 -0
- package/dist/maps/anchors.js.map +1 -0
- package/dist/maps/controller.d.ts +283 -0
- package/dist/maps/controller.d.ts.map +1 -0
- package/dist/maps/controller.js +743 -0
- package/dist/maps/controller.js.map +1 -0
- package/dist/maps/gl/buckets.d.ts +95 -0
- package/dist/maps/gl/buckets.d.ts.map +1 -0
- package/dist/maps/gl/buckets.js +362 -0
- package/dist/maps/gl/buckets.js.map +1 -0
- package/dist/maps/gl/build-worker.d.ts +13 -0
- package/dist/maps/gl/build-worker.d.ts.map +1 -0
- package/dist/maps/gl/build-worker.js +44 -0
- package/dist/maps/gl/build-worker.js.map +1 -0
- package/dist/maps/gl/color.d.ts +8 -0
- package/dist/maps/gl/color.d.ts.map +1 -0
- package/dist/maps/gl/color.js +57 -0
- package/dist/maps/gl/color.js.map +1 -0
- package/dist/maps/gl/cover.d.ts +51 -0
- package/dist/maps/gl/cover.d.ts.map +1 -0
- package/dist/maps/gl/cover.js +124 -0
- package/dist/maps/gl/cover.js.map +1 -0
- package/dist/maps/gl/index.d.ts +3 -0
- package/dist/maps/gl/index.d.ts.map +1 -0
- package/dist/maps/gl/index.js +20 -0
- package/dist/maps/gl/index.js.map +1 -0
- package/dist/maps/gl/markers.d.ts +28 -0
- package/dist/maps/gl/markers.d.ts.map +1 -0
- package/dist/maps/gl/markers.js +60 -0
- package/dist/maps/gl/markers.js.map +1 -0
- package/dist/maps/gl/overlays.d.ts +68 -0
- package/dist/maps/gl/overlays.d.ts.map +1 -0
- package/dist/maps/gl/overlays.js +300 -0
- package/dist/maps/gl/overlays.js.map +1 -0
- package/dist/maps/gl/pane.d.ts +12 -0
- package/dist/maps/gl/pane.d.ts.map +1 -0
- package/dist/maps/gl/pane.js +37 -0
- package/dist/maps/gl/pane.js.map +1 -0
- package/dist/maps/gl/placement.d.ts +111 -0
- package/dist/maps/gl/placement.d.ts.map +1 -0
- package/dist/maps/gl/placement.js +567 -0
- package/dist/maps/gl/placement.js.map +1 -0
- package/dist/maps/gl/renderer.d.ts +352 -0
- package/dist/maps/gl/renderer.d.ts.map +1 -0
- package/dist/maps/gl/renderer.js +1127 -0
- package/dist/maps/gl/renderer.js.map +1 -0
- package/dist/maps/gl/shaders.d.ts +120 -0
- package/dist/maps/gl/shaders.d.ts.map +1 -0
- package/dist/maps/gl/shaders.js +451 -0
- package/dist/maps/gl/shaders.js.map +1 -0
- package/dist/maps/gl/store.d.ts +115 -0
- package/dist/maps/gl/store.d.ts.map +1 -0
- package/dist/maps/gl/store.js +469 -0
- package/dist/maps/gl/store.js.map +1 -0
- package/dist/maps/gl/text.d.ts +154 -0
- package/dist/maps/gl/text.d.ts.map +1 -0
- package/dist/maps/gl/text.js +382 -0
- package/dist/maps/gl/text.js.map +1 -0
- package/dist/maps/gl/view.d.ts +54 -0
- package/dist/maps/gl/view.d.ts.map +1 -0
- package/dist/maps/gl/view.js +974 -0
- package/dist/maps/gl/view.js.map +1 -0
- package/dist/maps/index.d.ts +1 -1
- package/dist/maps/index.d.ts.map +1 -1
- package/dist/maps/index.js +216 -52
- package/dist/maps/index.js.map +1 -1
- package/dist/maps/labels.d.ts +37 -16
- package/dist/maps/labels.d.ts.map +1 -1
- package/dist/maps/labels.js +188 -119
- package/dist/maps/labels.js.map +1 -1
- package/dist/maps/node.d.ts +162 -56
- package/dist/maps/node.d.ts.map +1 -1
- package/dist/maps/node.js +607 -515
- package/dist/maps/node.js.map +1 -1
- package/dist/maps/overlay.d.ts +72 -9
- package/dist/maps/overlay.d.ts.map +1 -1
- package/dist/maps/overlay.js +108 -28
- package/dist/maps/overlay.js.map +1 -1
- package/dist/maps/paint.d.ts +3 -0
- package/dist/maps/paint.d.ts.map +1 -1
- package/dist/maps/paint.js.map +1 -1
- package/dist/maps/proj.d.ts +14 -0
- package/dist/maps/proj.d.ts.map +1 -1
- package/dist/maps/proj.js +28 -0
- package/dist/maps/proj.js.map +1 -1
- package/dist/maps/renderer.d.ts +69 -0
- package/dist/maps/renderer.d.ts.map +1 -0
- package/dist/maps/renderer.js +66 -0
- package/dist/maps/renderer.js.map +1 -0
- package/dist/maps/sources.d.ts +25 -3
- package/dist/maps/sources.d.ts.map +1 -1
- package/dist/maps/sources.js +17 -0
- package/dist/maps/sources.js.map +1 -1
- package/dist/maps/theme.d.ts +19 -0
- package/dist/maps/theme.d.ts.map +1 -0
- package/dist/maps/theme.js +44 -0
- package/dist/maps/theme.js.map +1 -0
- package/dist/maps/tiles.d.ts +117 -17
- package/dist/maps/tiles.d.ts.map +1 -1
- package/dist/maps/tiles.js +163 -30
- package/dist/maps/tiles.js.map +1 -1
- package/dist/maps/types.d.ts +188 -20
- package/dist/maps/types.d.ts.map +1 -1
- package/dist/markdown/expressions.d.ts +1 -1
- package/dist/markdown/expressions.d.ts.map +1 -1
- package/dist/markdown/expressions.js.map +1 -1
- package/dist/markdown/index.d.ts +7 -6
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +9 -7
- package/dist/markdown/index.js.map +1 -1
- package/dist/markdown/spans.d.ts +1 -1
- package/dist/markdown/spans.d.ts.map +1 -1
- package/dist/media-player/index.d.ts +11 -3
- package/dist/media-player/index.d.ts.map +1 -1
- package/dist/media-player/index.js +13 -2
- package/dist/media-player/index.js.map +1 -1
- package/dist/rich-text-editor/clipboard.d.ts +23 -0
- package/dist/rich-text-editor/clipboard.d.ts.map +1 -0
- package/dist/rich-text-editor/clipboard.js +307 -0
- package/dist/rich-text-editor/clipboard.js.map +1 -0
- package/dist/rich-text-editor/collab.d.ts +32 -0
- package/dist/rich-text-editor/collab.d.ts.map +1 -0
- package/dist/rich-text-editor/collab.js +55 -0
- package/dist/rich-text-editor/collab.js.map +1 -0
- package/dist/rich-text-editor/commands.d.ts +50 -0
- package/dist/rich-text-editor/commands.d.ts.map +1 -0
- package/dist/rich-text-editor/commands.js +376 -0
- package/dist/rich-text-editor/commands.js.map +1 -0
- package/dist/rich-text-editor/drag.d.ts +68 -0
- package/dist/rich-text-editor/drag.d.ts.map +1 -0
- package/dist/rich-text-editor/drag.js +148 -0
- package/dist/rich-text-editor/drag.js.map +1 -0
- package/dist/rich-text-editor/html.d.ts +145 -0
- package/dist/rich-text-editor/html.d.ts.map +1 -0
- package/dist/rich-text-editor/html.js +516 -0
- package/dist/rich-text-editor/html.js.map +1 -0
- package/dist/rich-text-editor/index.d.ts +263 -0
- package/dist/rich-text-editor/index.d.ts.map +1 -0
- package/dist/rich-text-editor/index.js +822 -0
- package/dist/rich-text-editor/index.js.map +1 -0
- package/dist/rich-text-editor/inline.d.ts +94 -0
- package/dist/rich-text-editor/inline.d.ts.map +1 -0
- package/dist/rich-text-editor/inline.js +252 -0
- package/dist/rich-text-editor/inline.js.map +1 -0
- package/dist/rich-text-editor/keymap.d.ts +71 -0
- package/dist/rich-text-editor/keymap.d.ts.map +1 -0
- package/dist/rich-text-editor/keymap.js +393 -0
- package/dist/rich-text-editor/keymap.js.map +1 -0
- package/dist/rich-text-editor/keys.d.ts +29 -0
- package/dist/rich-text-editor/keys.d.ts.map +1 -0
- package/dist/rich-text-editor/keys.js +93 -0
- package/dist/rich-text-editor/keys.js.map +1 -0
- package/dist/rich-text-editor/look.d.ts +71 -0
- package/dist/rich-text-editor/look.d.ts.map +1 -0
- package/dist/rich-text-editor/look.js +342 -0
- package/dist/rich-text-editor/look.js.map +1 -0
- package/dist/rich-text-editor/markdown.d.ts +18 -0
- package/dist/rich-text-editor/markdown.d.ts.map +1 -0
- package/dist/rich-text-editor/markdown.js +496 -0
- package/dist/rich-text-editor/markdown.js.map +1 -0
- package/dist/rich-text-editor/nodes.d.ts +135 -0
- package/dist/rich-text-editor/nodes.d.ts.map +1 -0
- package/dist/rich-text-editor/nodes.js +397 -0
- package/dist/rich-text-editor/nodes.js.map +1 -0
- package/dist/rich-text-editor/render.d.ts +68 -0
- package/dist/rich-text-editor/render.d.ts.map +1 -0
- package/dist/rich-text-editor/render.js +500 -0
- package/dist/rich-text-editor/render.js.map +1 -0
- package/dist/rich-text-editor/schema.d.ts +16 -0
- package/dist/rich-text-editor/schema.d.ts.map +1 -0
- package/dist/rich-text-editor/schema.js +386 -0
- package/dist/rich-text-editor/schema.js.map +1 -0
- package/dist/rich-text-editor/store.d.ts +11 -0
- package/dist/rich-text-editor/store.d.ts.map +1 -0
- package/dist/rich-text-editor/store.js +43 -0
- package/dist/rich-text-editor/store.js.map +1 -0
- package/dist/rich-text-editor/suggest.d.ts +127 -0
- package/dist/rich-text-editor/suggest.d.ts.map +1 -0
- package/dist/rich-text-editor/suggest.js +0 -0
- package/dist/rich-text-editor/suggest.js.map +1 -0
- package/dist/rich-text-editor/tables.d.ts +41 -0
- package/dist/rich-text-editor/tables.d.ts.map +1 -0
- package/dist/rich-text-editor/tables.js +204 -0
- package/dist/rich-text-editor/tables.js.map +1 -0
- package/dist/rich-text-editor/toolbar.d.ts +58 -0
- package/dist/rich-text-editor/toolbar.d.ts.map +1 -0
- package/dist/rich-text-editor/toolbar.js +548 -0
- package/dist/rich-text-editor/toolbar.js.map +1 -0
- package/dist/rich-text-editor/view.d.ts +368 -0
- package/dist/rich-text-editor/view.d.ts.map +1 -0
- package/dist/rich-text-editor/view.js +1968 -0
- package/dist/rich-text-editor/view.js.map +1 -0
- package/dist/rich-text-editor/virtual.d.ts +54 -0
- package/dist/rich-text-editor/virtual.d.ts.map +1 -0
- package/dist/rich-text-editor/virtual.js +262 -0
- package/dist/rich-text-editor/virtual.js.map +1 -0
- package/dist/richtext/node.d.ts +25 -2
- package/dist/richtext/node.d.ts.map +1 -1
- package/dist/richtext/node.js +46 -24
- package/dist/richtext/node.js.map +1 -1
- package/dist/table/index.d.ts.map +1 -1
- package/dist/table/index.js +14 -5
- package/dist/table/index.js.map +1 -1
- package/dist/terminal/backends.d.ts +4 -1
- package/dist/terminal/backends.d.ts.map +1 -1
- package/dist/terminal/backends.js.map +1 -1
- package/dist/terminal/index.d.ts +18 -7
- package/dist/terminal/index.d.ts.map +1 -1
- package/dist/terminal/index.js +28 -11
- package/dist/terminal/index.js.map +1 -1
- package/dist/terminal/vt/node.d.ts +56 -3
- package/dist/terminal/vt/node.d.ts.map +1 -1
- package/dist/terminal/vt/node.js +106 -26
- package/dist/terminal/vt/node.js.map +1 -1
- package/dist/terminal/vt/renderer.d.ts +4 -0
- package/dist/terminal/vt/renderer.d.ts.map +1 -1
- package/dist/terminal/vt/renderer.js +14 -5
- package/dist/terminal/vt/renderer.js.map +1 -1
- package/dist/tree/index.d.ts.map +1 -1
- package/dist/tree/index.js +6 -4
- package/dist/tree/index.js.map +1 -1
- package/package.json +23 -4
- package/src/charts/node.ts +6 -2
- package/src/color-picker/ColorPicker.ts +107 -81
- package/src/embed/client.ts +102 -8
- package/src/embed/index.ts +2 -0
- package/src/html/node.ts +7 -5
- package/src/index.ts +2 -0
- package/src/{markdown → internal/markdown}/ast.ts +24 -7
- package/src/{markdown → internal/markdown}/parse.ts +134 -24
- package/src/internal/markdown/stringify.ts +772 -0
- package/src/internal/scroll.ts +39 -18
- package/src/internal/units.ts +34 -0
- package/src/internal/window.ts +4 -2
- package/src/maps/anchors.ts +607 -0
- package/src/maps/controller.ts +999 -0
- package/src/maps/gl/buckets.ts +457 -0
- package/src/maps/gl/build-worker.ts +55 -0
- package/src/maps/gl/color.ts +62 -0
- package/src/maps/gl/cover.ts +170 -0
- package/src/maps/gl/index.ts +23 -0
- package/src/maps/gl/markers.ts +83 -0
- package/src/maps/gl/overlays.ts +379 -0
- package/src/maps/gl/pane.ts +42 -0
- package/src/maps/gl/placement.ts +816 -0
- package/src/maps/gl/renderer.ts +1576 -0
- package/src/maps/gl/shaders.ts +486 -0
- package/src/maps/gl/store.ts +566 -0
- package/src/maps/gl/text.ts +495 -0
- package/src/maps/gl/view.ts +1224 -0
- package/src/maps/index.ts +270 -81
- package/src/maps/labels.ts +214 -121
- package/src/maps/node.ts +763 -629
- package/src/maps/overlay.ts +148 -43
- package/src/maps/paint.ts +3 -0
- package/src/maps/proj.ts +31 -0
- package/src/maps/renderer.ts +155 -0
- package/src/maps/sources.ts +38 -3
- package/src/maps/theme.ts +51 -0
- package/src/maps/tiles.ts +191 -33
- package/src/maps/types.ts +188 -20
- package/src/markdown/expressions.ts +5 -1
- package/src/markdown/index.ts +14 -11
- package/src/markdown/spans.ts +1 -1
- package/src/media-player/index.ts +18 -4
- package/src/rich-text-editor/clipboard.ts +439 -0
- package/src/rich-text-editor/collab.ts +91 -0
- package/src/rich-text-editor/commands.ts +448 -0
- package/src/rich-text-editor/dom-globals.d.ts +23 -0
- package/src/rich-text-editor/drag.ts +211 -0
- package/src/rich-text-editor/html.ts +619 -0
- package/src/rich-text-editor/index.ts +1331 -0
- package/src/rich-text-editor/inline.ts +345 -0
- package/src/rich-text-editor/keymap.ts +510 -0
- package/src/rich-text-editor/keys.ts +119 -0
- package/src/rich-text-editor/look.ts +427 -0
- package/src/rich-text-editor/markdown.ts +653 -0
- package/src/rich-text-editor/nodes.ts +497 -0
- package/src/rich-text-editor/render.ts +800 -0
- package/src/rich-text-editor/schema.ts +431 -0
- package/src/rich-text-editor/store.ts +39 -0
- package/src/rich-text-editor/suggest.ts +597 -0
- package/src/rich-text-editor/tables.ts +272 -0
- package/src/rich-text-editor/toolbar.ts +724 -0
- package/src/rich-text-editor/view.ts +2287 -0
- package/src/rich-text-editor/virtual.ts +347 -0
- package/src/richtext/node.ts +59 -30
- package/src/table/index.ts +14 -5
- package/src/terminal/backends.ts +4 -1
- package/src/terminal/index.ts +43 -15
- package/src/terminal/vt/node.ts +128 -29
- package/src/terminal/vt/renderer.ts +27 -5
- package/src/tree/index.ts +6 -4
- package/dist/markdown/ast.d.ts.map +0 -1
- package/dist/markdown/ast.js +0 -17
- package/dist/markdown/ast.js.map +0 -1
- package/dist/markdown/parse.d.ts.map +0 -1
- package/dist/markdown/parse.js.map +0 -1
- package/dist/markdown/tags.d.ts.map +0 -1
- package/dist/markdown/tags.js.map +0 -1
- /package/dist/{markdown → internal/markdown}/parse.d.ts +0 -0
- /package/dist/{markdown → internal/markdown}/tags.d.ts +0 -0
- /package/dist/{markdown → internal/markdown}/tags.js +0 -0
- /package/src/{markdown → internal/markdown}/tags.ts +0 -0
|
@@ -0,0 +1,1576 @@
|
|
|
1
|
+
// The GL renderer: every frame drawn from geometry, nothing cached as pixels.
|
|
2
|
+
//
|
|
3
|
+
// The retained renderer's frame composites tile *surfaces*; this one's frame
|
|
4
|
+
// is the whole style drawn again from each tile's buckets (`./buckets.ts`),
|
|
5
|
+
// which is a few hundred draw calls and a few uniform writes per call. Pan,
|
|
6
|
+
// zoom and restyle are therefore the same operation — draw — and a
|
|
7
|
+
// fractional zoom is not a scaled bitmap but the vector map at that zoom,
|
|
8
|
+
// line widths included.
|
|
9
|
+
//
|
|
10
|
+
// The frame is **layer-major**: for each style layer, every tile's share of
|
|
11
|
+
// it, before the next layer. That is what the cartography needs — every
|
|
12
|
+
// tile's water under any tile's roads, every casing under any fill, so
|
|
13
|
+
// junctions knit across tile seams exactly as they do inside a tile — and it
|
|
14
|
+
// is cheap because each tile is **scissored** to its own square. The camera
|
|
15
|
+
// has no rotation, so a tile's footprint on screen is an axis-aligned
|
|
16
|
+
// rectangle, and a scissor is a clip that costs one call and no stencil.
|
|
17
|
+
// Tiles carry a buffer of geometry past their edges; the scissor is what
|
|
18
|
+
// keeps two tiles from drawing the same road twice.
|
|
19
|
+
//
|
|
20
|
+
// Two things a caller can ask of a frame beyond "the style, here": that it
|
|
21
|
+
// be **cheaper** (`detail`, `edges` — adaptive quality's levers, priced in
|
|
22
|
+
// advance by `estimate`), and that a second scene be **faded in** over it
|
|
23
|
+
// (`render(frame, fade)` — the cross-fade between two pyramid levels, drawn
|
|
24
|
+
// into an offscreen target and composited once).
|
|
25
|
+
import type { PreparedStyle } from '../paint.js';
|
|
26
|
+
import type { MapStyleLayer } from '../style.js';
|
|
27
|
+
import { resolveZoomed } from '../style.js';
|
|
28
|
+
import { RECORD_BYTES, TILE_EXTENT } from './buckets.js';
|
|
29
|
+
import type { GlTileData } from './buckets.js';
|
|
30
|
+
import { parseColor, premultiplied } from './color.js';
|
|
31
|
+
import type { Rgba } from './color.js';
|
|
32
|
+
import { MARKER_INSTANCE } from './markers.js';
|
|
33
|
+
import type { MarkerBatch } from './markers.js';
|
|
34
|
+
import type { OverlayPass } from './overlays.js';
|
|
35
|
+
import { LABEL_INSTANCE } from './placement.js';
|
|
36
|
+
import type { LabelBatch } from './placement.js';
|
|
37
|
+
import {
|
|
38
|
+
ATTRIBUTES,
|
|
39
|
+
BLIT_FRAGMENT,
|
|
40
|
+
BLIT_VERTEX,
|
|
41
|
+
CIRCLE_FRAGMENT,
|
|
42
|
+
CIRCLE_VERTEX,
|
|
43
|
+
COVER_FRAGMENT,
|
|
44
|
+
COVER_VERTEX,
|
|
45
|
+
FAN_FRAGMENT,
|
|
46
|
+
FAN_VERTEX,
|
|
47
|
+
LABEL_FRAGMENT,
|
|
48
|
+
LABEL_VERTEX,
|
|
49
|
+
LINE_FRAGMENT,
|
|
50
|
+
LINE_VERTEX,
|
|
51
|
+
MARKER_FRAGMENT,
|
|
52
|
+
MARKER_VERTEX,
|
|
53
|
+
RASTER_FRAGMENT,
|
|
54
|
+
RASTER_VERTEX,
|
|
55
|
+
linkProgram,
|
|
56
|
+
} from './shaders.js';
|
|
57
|
+
import type { GL, Program } from './shaders.js';
|
|
58
|
+
import { quadInset } from './text.js';
|
|
59
|
+
import type { LabelAtlas } from './text.js';
|
|
60
|
+
|
|
61
|
+
/** A rectangle in device pixels, top-left origin. */
|
|
62
|
+
export interface DeviceRect {
|
|
63
|
+
x: number;
|
|
64
|
+
y: number;
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** One tile's data placed on screen. */
|
|
70
|
+
export interface RenderTile {
|
|
71
|
+
data: GlTileData;
|
|
72
|
+
/** Where the data tile's square lands, in device pixels: its top-left
|
|
73
|
+
* corner and its edge length. */
|
|
74
|
+
x: number;
|
|
75
|
+
y: number;
|
|
76
|
+
size: number;
|
|
77
|
+
/**
|
|
78
|
+
* The part of the viewport this tile draws into. Its own square, unless
|
|
79
|
+
* it is standing in for a tile that has no data yet — an ancestor scaled
|
|
80
|
+
* up, drawn only over the square it covers for.
|
|
81
|
+
*/
|
|
82
|
+
clip: DeviceRect;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Everything a frame is drawn from. */
|
|
86
|
+
export interface RenderFrame {
|
|
87
|
+
/** Device pixels. */
|
|
88
|
+
width: number;
|
|
89
|
+
height: number;
|
|
90
|
+
/** The camera zoom: what zoom ramps and layer ranges resolve at. */
|
|
91
|
+
zoom: number;
|
|
92
|
+
/** Device pixels per logical pixel: what style lengths are multiplied by. */
|
|
93
|
+
scale: number;
|
|
94
|
+
style: PreparedStyle;
|
|
95
|
+
background?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Each source's tiles, in the map's order — a basemap, then a pyramid
|
|
98
|
+
* over it. Each is drawn whole, layer-major, before the next, which is
|
|
99
|
+
* how the retained renderer lays one source's tiles over another's.
|
|
100
|
+
*/
|
|
101
|
+
sources: readonly (readonly RenderTile[])[];
|
|
102
|
+
/**
|
|
103
|
+
* Leave out the style layers that begin within this many levels of the
|
|
104
|
+
* zoom: a layer whose `minZoom` is above `zoom - detail` is skipped. `0`
|
|
105
|
+
* (the default) draws what the style says.
|
|
106
|
+
*
|
|
107
|
+
* The layers a style brings in last as the map zooms in are its details —
|
|
108
|
+
* buildings, service roads, paths, sites — so this drops exactly those, in
|
|
109
|
+
* that order, and it needs nothing from the style that the style has not
|
|
110
|
+
* already said.
|
|
111
|
+
*/
|
|
112
|
+
detail?: number;
|
|
113
|
+
/** The fill edge pass for this frame, over the renderer's `antialias`. */
|
|
114
|
+
edges?: boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A second scene, and how much of it to show over the first. */
|
|
118
|
+
export interface FadeFrame {
|
|
119
|
+
frame: RenderFrame;
|
|
120
|
+
/** `0` shows only the base scene, `1` only this one. */
|
|
121
|
+
alpha: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The attribution: a box of the theme's background, and the text in it —
|
|
126
|
+
* one label's worth of instance data from the labels' atlas, set level and
|
|
127
|
+
* on whole pixels.
|
|
128
|
+
*/
|
|
129
|
+
export interface AttributionDraw {
|
|
130
|
+
/** Device pixels, top-left origin. */
|
|
131
|
+
box: DeviceRect;
|
|
132
|
+
/** Premultiplied, its opacity folded in. */
|
|
133
|
+
boxColor: Rgba;
|
|
134
|
+
text: LabelBatch;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The overlays' bucket, and where its units land this frame. */
|
|
138
|
+
export interface OverlayDraw {
|
|
139
|
+
data: GlTileData;
|
|
140
|
+
passes: readonly OverlayPass[];
|
|
141
|
+
/** Device pixels per unit of the bucket's records. */
|
|
142
|
+
unit: number;
|
|
143
|
+
/** Where the bucket's origin lands, in device pixels. */
|
|
144
|
+
x: number;
|
|
145
|
+
y: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** What a frame draws over its scene, in this order. */
|
|
149
|
+
export interface RenderExtras {
|
|
150
|
+
/** A second scene, faded in over the first. */
|
|
151
|
+
fade?: FadeFrame | null;
|
|
152
|
+
/** The labels, over both. */
|
|
153
|
+
labels?: LabelBatch | null;
|
|
154
|
+
/** The overlays, over the labels — where the retained renderer draws
|
|
155
|
+
* them. */
|
|
156
|
+
overlays?: OverlayDraw | null;
|
|
157
|
+
/** The markers, over the labels — where the retained renderer draws
|
|
158
|
+
* them, a marker being what the user aims at. */
|
|
159
|
+
markers?: MarkerBatch | null;
|
|
160
|
+
/** The attribution, over everything — drawn last, as the retained
|
|
161
|
+
* renderer draws it. */
|
|
162
|
+
attribution?: AttributionDraw | null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface GlRenderOptions {
|
|
166
|
+
/**
|
|
167
|
+
* Draw each polygon's boundary as a half-pixel antialiased line after the
|
|
168
|
+
* fill. `true` by default. There is no multisampling on these surfaces
|
|
169
|
+
* (x11-dri exposes no `renderbufferStorageMultisample`), so without it a
|
|
170
|
+
* fill's edge is a staircase; this is the same trade MapLibre's
|
|
171
|
+
* `fill-antialias` makes.
|
|
172
|
+
*/
|
|
173
|
+
antialias?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* `'nonzero'` (the default) is the rule the retained renderer fills with,
|
|
176
|
+
* and costs two stencil passes, one per facing — or one, on a GL table
|
|
177
|
+
* with `stencilOpSeparate` (x11-dri 0.8, and the Cocoa backend's), which
|
|
178
|
+
* counts both facings in the same draw. `'evenodd'` is one pass —
|
|
179
|
+
* and wrong wherever two features of one layer overlap, which real
|
|
180
|
+
* landuse does: the overlap is cut out as if it were a hole.
|
|
181
|
+
*/
|
|
182
|
+
fillRule?: 'nonzero' | 'evenodd';
|
|
183
|
+
/**
|
|
184
|
+
* Draw into an offscreen framebuffer and copy it to the surface. `'auto'`
|
|
185
|
+
* (the default) does that only when the surface has no stencil buffer —
|
|
186
|
+
* which is XQuartz's Apple-DRI surface today, whose context ntk creates
|
|
187
|
+
* without one.
|
|
188
|
+
*/
|
|
189
|
+
offscreen?: 'auto' | boolean;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** What one frame cost, and what it drew. */
|
|
193
|
+
export interface GlRenderStats {
|
|
194
|
+
tiles: number;
|
|
195
|
+
layers: number;
|
|
196
|
+
drawCalls: number;
|
|
197
|
+
/** Segment instances drawn, every pass counted — the frame's work. */
|
|
198
|
+
instances: number;
|
|
199
|
+
/** Tiles uploaded this frame, and their bytes. */
|
|
200
|
+
uploads: number;
|
|
201
|
+
uploadBytes: number;
|
|
202
|
+
/** Milliseconds on this thread issuing the frame — not the GPU's time. */
|
|
203
|
+
cpuMs: number;
|
|
204
|
+
offscreen: boolean;
|
|
205
|
+
/** How much of a second scene was faded in; `0` when there was none. */
|
|
206
|
+
fade: number;
|
|
207
|
+
/** Labels drawn. */
|
|
208
|
+
labels: number;
|
|
209
|
+
/** Markers drawn. */
|
|
210
|
+
markers: number;
|
|
211
|
+
/** Overlay passes drawn — a line with a casing is two. */
|
|
212
|
+
overlays: number;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A stroke: premultiplied colour, half its width in device pixels, and its
|
|
216
|
+
* dash pattern as {@link dashPattern} gives one. */
|
|
217
|
+
interface StrokePaint {
|
|
218
|
+
color: Rgba;
|
|
219
|
+
half: number;
|
|
220
|
+
dash: readonly number[] | null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Entries of a dash pattern the line program holds: four dashes. */
|
|
224
|
+
const DASH_ENTRIES = 8;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* A style's or an overlay's dash array as the line program draws it: in
|
|
228
|
+
* device pixels, and read as a canvas reads one — an odd number of entries
|
|
229
|
+
* is repeated to make an even one, and an entry that is negative or not a
|
|
230
|
+
* number, or entries that are all zero, make a solid line. A pattern of
|
|
231
|
+
* more than four dashes is cut to its first four.
|
|
232
|
+
*/
|
|
233
|
+
export function dashPattern(
|
|
234
|
+
dash: readonly number[] | undefined,
|
|
235
|
+
scale: number,
|
|
236
|
+
): number[] | null {
|
|
237
|
+
if (!dash || dash.length === 0) return null;
|
|
238
|
+
if (dash.some((d) => !Number.isFinite(d) || d < 0)) return null;
|
|
239
|
+
if (!dash.some((d) => d > 0)) return null;
|
|
240
|
+
const even = dash.length % 2 === 1 ? [...dash, ...dash] : dash;
|
|
241
|
+
return even.slice(0, DASH_ENTRIES).map((d) => d * scale);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** A polygon: its colour, and its edge — the antialiasing, or an
|
|
245
|
+
* outline. */
|
|
246
|
+
interface FillPaint {
|
|
247
|
+
color: Rgba;
|
|
248
|
+
antialias: boolean;
|
|
249
|
+
outline: Rgba | null;
|
|
250
|
+
outlineHalf: number;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const CLEAR: Rgba = [0, 0, 0, 0];
|
|
254
|
+
|
|
255
|
+
interface TileGpu {
|
|
256
|
+
line: unknown;
|
|
257
|
+
fill: unknown;
|
|
258
|
+
/** One vertex array per range start and stream: `first * 2 + (fill ? 1 : 0)`. */
|
|
259
|
+
vaos: Map<number, unknown>;
|
|
260
|
+
/** A raster tile's image. */
|
|
261
|
+
texture: unknown;
|
|
262
|
+
bytes: number;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
interface Offscreen {
|
|
266
|
+
fbo: unknown;
|
|
267
|
+
texture: unknown;
|
|
268
|
+
depth: unknown;
|
|
269
|
+
width: number;
|
|
270
|
+
height: number;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const globals = globalThis as { performance?: { now(): number } };
|
|
274
|
+
const now = (): number => globals.performance?.now() ?? Date.now();
|
|
275
|
+
|
|
276
|
+
/** Whether a style layer draws at this zoom, with `detail` levels of its
|
|
277
|
+
* newest layers left out — `paint.ts`'s gate, plus adaptive quality's. */
|
|
278
|
+
function drawsAt(layer: MapStyleLayer, zoom: number, detail: number): boolean {
|
|
279
|
+
if (layer.visible === false) return false;
|
|
280
|
+
if (
|
|
281
|
+
layer.type !== 'fill' &&
|
|
282
|
+
layer.type !== 'line' &&
|
|
283
|
+
layer.type !== 'circle'
|
|
284
|
+
) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (layer.minZoom !== undefined && zoom < layer.minZoom) return false;
|
|
288
|
+
if (layer.maxZoom !== undefined && zoom >= layer.maxZoom) return false;
|
|
289
|
+
if (
|
|
290
|
+
detail > 0 &&
|
|
291
|
+
layer.minZoom !== undefined &&
|
|
292
|
+
layer.minZoom > zoom - detail
|
|
293
|
+
) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Label rasters taken into the atlas texture per frame at most. */
|
|
300
|
+
const ATLAS_UPLOADS_PER_FRAME = 24;
|
|
301
|
+
|
|
302
|
+
/** GL's own name for "which VAO is bound", which the table may not carry. */
|
|
303
|
+
const VERTEX_ARRAY_BINDING = 0x85b5;
|
|
304
|
+
const FRAMEBUFFER_BINDING = 0x8ca6;
|
|
305
|
+
|
|
306
|
+
export class GlMapRenderer {
|
|
307
|
+
private readonly _gl: GL;
|
|
308
|
+
private readonly _options: Required<GlRenderOptions>;
|
|
309
|
+
private readonly _line: Program;
|
|
310
|
+
private readonly _fan: Program;
|
|
311
|
+
private readonly _cover: Program;
|
|
312
|
+
private _blit: Program | null = null;
|
|
313
|
+
private readonly _corners: unknown;
|
|
314
|
+
private readonly _coverVao: unknown;
|
|
315
|
+
private readonly _tiles = new Map<GlTileData, TileGpu>();
|
|
316
|
+
private readonly _colors = new Map<string, Rgba | null>();
|
|
317
|
+
private _stencil: boolean | null = null;
|
|
318
|
+
/** Whether the table sets each facing's stencil operation apart, which
|
|
319
|
+
* makes the non-zero winding one pass. */
|
|
320
|
+
private readonly _separate: boolean;
|
|
321
|
+
/** Offscreen targets by role: `'output'` stands in for a surface with no
|
|
322
|
+
* stencil buffer, `'fade'` holds the scene being faded in. */
|
|
323
|
+
private readonly _targets = new Map<string, Offscreen>();
|
|
324
|
+
/** The label pass: its program, its instance buffer and the vertex array
|
|
325
|
+
* over it, and the atlas texture with the atlas generation it holds. */
|
|
326
|
+
private _label: Program | null = null;
|
|
327
|
+
private _labelVao: unknown = null;
|
|
328
|
+
private _labelBuffer: unknown = null;
|
|
329
|
+
private _marker: Program | null = null;
|
|
330
|
+
private _markerVao: unknown = null;
|
|
331
|
+
private _markerBuffer: unknown = null;
|
|
332
|
+
private _circle: Program | null = null;
|
|
333
|
+
private _raster: Program | null = null;
|
|
334
|
+
private _atlasTexture: unknown = null;
|
|
335
|
+
private _atlasOwner: LabelAtlas | null = null;
|
|
336
|
+
private _stats: GlRenderStats = GlMapRenderer._emptyStats();
|
|
337
|
+
/** Bytes of tile geometry resident on the GPU. */
|
|
338
|
+
gpuBytes = 0;
|
|
339
|
+
|
|
340
|
+
constructor(gl: GL, options: GlRenderOptions = {}) {
|
|
341
|
+
this._gl = gl;
|
|
342
|
+
this._options = {
|
|
343
|
+
antialias: options.antialias ?? true,
|
|
344
|
+
fillRule: options.fillRule ?? 'nonzero',
|
|
345
|
+
offscreen: options.offscreen ?? 'auto',
|
|
346
|
+
};
|
|
347
|
+
// Asked of the table itself: an x11-dri from before 0.8 has no such
|
|
348
|
+
// entry at all, and an entry that is there works.
|
|
349
|
+
this._separate =
|
|
350
|
+
'stencilOpSeparate' in (gl as object) &&
|
|
351
|
+
typeof gl.stencilOpSeparate === 'function';
|
|
352
|
+
this._line = linkProgram(gl, LINE_VERTEX, LINE_FRAGMENT, [
|
|
353
|
+
'u_tile',
|
|
354
|
+
'u_viewport',
|
|
355
|
+
'u_half',
|
|
356
|
+
'u_color',
|
|
357
|
+
'u_dash_a',
|
|
358
|
+
'u_dash_b',
|
|
359
|
+
'u_period',
|
|
360
|
+
'u_part',
|
|
361
|
+
]);
|
|
362
|
+
this._fan = linkProgram(gl, FAN_VERTEX, FAN_FRAGMENT, [
|
|
363
|
+
'u_tile',
|
|
364
|
+
'u_viewport',
|
|
365
|
+
]);
|
|
366
|
+
this._cover = linkProgram(gl, COVER_VERTEX, COVER_FRAGMENT, ['u_color']);
|
|
367
|
+
|
|
368
|
+
const previous = gl.getParameter(VERTEX_ARRAY_BINDING);
|
|
369
|
+
// The quad every instance is drawn over. A capsule is a four-vertex
|
|
370
|
+
// strip; a fan triangle takes the first three corners.
|
|
371
|
+
this._corners = gl.createBuffer();
|
|
372
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._corners);
|
|
373
|
+
gl.bufferData(
|
|
374
|
+
gl.ARRAY_BUFFER,
|
|
375
|
+
new Float32Array([0, -1, 1, -1, 0, 1, 1, 1]),
|
|
376
|
+
gl.STATIC_DRAW,
|
|
377
|
+
);
|
|
378
|
+
this._coverVao = gl.createVertexArray();
|
|
379
|
+
gl.bindVertexArray(this._coverVao);
|
|
380
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_corner);
|
|
381
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_corner, 2, gl.FLOAT, false, 0, 0);
|
|
382
|
+
gl.bindVertexArray(previous);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
set antialias(on: boolean) {
|
|
386
|
+
this._options.antialias = on;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
set fillRule(rule: 'nonzero' | 'evenodd') {
|
|
390
|
+
this._options.fillRule = rule;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private static _emptyStats(): GlRenderStats {
|
|
394
|
+
return {
|
|
395
|
+
tiles: 0,
|
|
396
|
+
layers: 0,
|
|
397
|
+
drawCalls: 0,
|
|
398
|
+
instances: 0,
|
|
399
|
+
uploads: 0,
|
|
400
|
+
uploadBytes: 0,
|
|
401
|
+
cpuMs: 0,
|
|
402
|
+
offscreen: false,
|
|
403
|
+
fade: 0,
|
|
404
|
+
labels: 0,
|
|
405
|
+
markers: 0,
|
|
406
|
+
overlays: 0,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* What a frame would cost, in the unit `stats.instances` counts — segment
|
|
412
|
+
* instances, every pass included — without drawing it. The same gates the
|
|
413
|
+
* frame itself applies, over the ranges' record counts, so it is a walk of
|
|
414
|
+
* a few hundred numbers: cheap enough to price several versions of the
|
|
415
|
+
* next frame and draw the one that fits.
|
|
416
|
+
*/
|
|
417
|
+
estimate(frame: RenderFrame): number {
|
|
418
|
+
const { width, height } = frame;
|
|
419
|
+
const layers = frame.style.layers;
|
|
420
|
+
const detail = frame.detail ?? 0;
|
|
421
|
+
const edges = frame.edges ?? this._options.antialias;
|
|
422
|
+
const stencil =
|
|
423
|
+
this._options.fillRule === 'nonzero' && !this._separate ? 2 : 1;
|
|
424
|
+
let work = 0;
|
|
425
|
+
for (const tiles of frame.sources) {
|
|
426
|
+
const visible = tiles.map(
|
|
427
|
+
(t) => scissorOf(t.clip, width, height) !== null,
|
|
428
|
+
);
|
|
429
|
+
for (let i = 0; i < layers.length; i++) {
|
|
430
|
+
const layer = layers[i].layer;
|
|
431
|
+
if (!drawsAt(layer, frame.zoom, detail)) continue;
|
|
432
|
+
let records = 0;
|
|
433
|
+
for (let t = 0; t < tiles.length; t++) {
|
|
434
|
+
if (!visible[t]) continue;
|
|
435
|
+
const draw = tiles[t].data.draws[i];
|
|
436
|
+
if (!draw) continue;
|
|
437
|
+
for (let r = 1; r < draw.ranges.length; r += 2) {
|
|
438
|
+
records += draw.ranges[r] - 1;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (layer.type === 'fill') {
|
|
442
|
+
const edge = layer.outlineColor !== undefined || edges ? 1 : 0;
|
|
443
|
+
work += records * (stencil + edge);
|
|
444
|
+
} else {
|
|
445
|
+
work += records;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return work;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Draw a frame into whatever framebuffer is bound — and, given `fade`, a
|
|
454
|
+
* second scene over it at `fade.alpha`, and given `labels`, the labels
|
|
455
|
+
* over both.
|
|
456
|
+
*
|
|
457
|
+
* The second scene is drawn whole into an offscreen target and composited
|
|
458
|
+
* once, rather than layer by layer at reduced opacity: a map faded layer
|
|
459
|
+
* by layer shows its casings through its fills and its water through its
|
|
460
|
+
* land. Both scenes are opaque, so the composite is exactly a cross-fade.
|
|
461
|
+
* Labels go on after it, at full strength: they are placed for the view,
|
|
462
|
+
* not for either level, and a name that dimmed with the level it came
|
|
463
|
+
* from would blink at every level change.
|
|
464
|
+
*/
|
|
465
|
+
render(frame: RenderFrame, extras: RenderExtras = {}): GlRenderStats {
|
|
466
|
+
const { fade, labels, overlays, markers, attribution } = extras;
|
|
467
|
+
const gl = this._gl;
|
|
468
|
+
const started = now();
|
|
469
|
+
const stats = (this._stats = GlMapRenderer._emptyStats());
|
|
470
|
+
const previousVao = gl.getParameter(VERTEX_ARRAY_BINDING);
|
|
471
|
+
const surface = gl.getParameter(FRAMEBUFFER_BINDING);
|
|
472
|
+
const { width, height } = frame;
|
|
473
|
+
|
|
474
|
+
// Before the probe: it draws, and a context that has never had a
|
|
475
|
+
// viewport set — a windowless one, bound to a framebuffer — has a 0×0
|
|
476
|
+
// one, which draws nothing and reads as "no stencil".
|
|
477
|
+
gl.viewport(0, 0, width, height);
|
|
478
|
+
if (this._stencil === null) this._stencil = this._probeStencil();
|
|
479
|
+
const mode = this._options.offscreen;
|
|
480
|
+
const offscreen = mode === true || (mode === 'auto' && !this._stencil);
|
|
481
|
+
stats.offscreen = offscreen;
|
|
482
|
+
const output = offscreen ? this._target('output', width, height) : null;
|
|
483
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, output ? output.fbo : surface);
|
|
484
|
+
|
|
485
|
+
this._scene(frame);
|
|
486
|
+
|
|
487
|
+
if (fade && fade.alpha > 0) {
|
|
488
|
+
const layer = this._target('fade', width, height);
|
|
489
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, layer.fbo);
|
|
490
|
+
this._scene(fade.frame);
|
|
491
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, output ? output.fbo : surface);
|
|
492
|
+
this._composite(layer, width, height, Math.min(1, fade.alpha));
|
|
493
|
+
stats.fade = Math.min(1, fade.alpha);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Even with nothing to draw: the batch's atlas may have rasters waiting
|
|
497
|
+
// for the texture, and until they are in it nothing of theirs can be.
|
|
498
|
+
if (labels) stats.labels = this._labels(labels, width, height);
|
|
499
|
+
|
|
500
|
+
if (overlays) stats.overlays = this._overlays(overlays, frame);
|
|
501
|
+
|
|
502
|
+
if (markers) stats.markers = this._markers(markers, width, height);
|
|
503
|
+
|
|
504
|
+
// Last, over the labels and everything else, which is where the
|
|
505
|
+
// retained renderer draws it: a licence condition is not something a
|
|
506
|
+
// street name may cover.
|
|
507
|
+
if (attribution) this._attribution(attribution, width, height);
|
|
508
|
+
|
|
509
|
+
if (output) {
|
|
510
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, surface);
|
|
511
|
+
this._composite(output, width, height, 1);
|
|
512
|
+
}
|
|
513
|
+
gl.bindVertexArray(previousVao);
|
|
514
|
+
stats.cpuMs = now() - started;
|
|
515
|
+
return stats;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** Drop one tile's buffers. */
|
|
519
|
+
release(data: GlTileData): void {
|
|
520
|
+
const gpu = this._tiles.get(data);
|
|
521
|
+
if (!gpu) return;
|
|
522
|
+
const gl = this._gl;
|
|
523
|
+
for (const vao of gpu.vaos.values()) gl.deleteVertexArray(vao);
|
|
524
|
+
if (gpu.line) gl.deleteBuffer(gpu.line);
|
|
525
|
+
if (gpu.fill) gl.deleteBuffer(gpu.fill);
|
|
526
|
+
if (gpu.texture) gl.deleteTexture(gpu.texture);
|
|
527
|
+
this.gpuBytes -= gpu.bytes;
|
|
528
|
+
this._tiles.delete(data);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Whether a tile's buffers are resident. */
|
|
532
|
+
resident(data: GlTileData): boolean {
|
|
533
|
+
return this._tiles.has(data);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
dispose(): void {
|
|
537
|
+
const gl = this._gl;
|
|
538
|
+
for (const data of [...this._tiles.keys()]) this.release(data);
|
|
539
|
+
for (const program of [
|
|
540
|
+
this._line,
|
|
541
|
+
this._fan,
|
|
542
|
+
this._cover,
|
|
543
|
+
this._blit,
|
|
544
|
+
this._label,
|
|
545
|
+
this._marker,
|
|
546
|
+
this._circle,
|
|
547
|
+
this._raster,
|
|
548
|
+
]) {
|
|
549
|
+
if (program) gl.deleteProgram(program.program);
|
|
550
|
+
}
|
|
551
|
+
gl.deleteBuffer(this._corners);
|
|
552
|
+
gl.deleteVertexArray(this._coverVao);
|
|
553
|
+
if (this._labelVao) gl.deleteVertexArray(this._labelVao);
|
|
554
|
+
if (this._labelBuffer) gl.deleteBuffer(this._labelBuffer);
|
|
555
|
+
if (this._markerVao) gl.deleteVertexArray(this._markerVao);
|
|
556
|
+
if (this._markerBuffer) gl.deleteBuffer(this._markerBuffer);
|
|
557
|
+
if (this._atlasTexture) gl.deleteTexture(this._atlasTexture);
|
|
558
|
+
for (const target of this._targets.values()) this._dropTarget(target);
|
|
559
|
+
this._targets.clear();
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// --- the scene ------------------------------------------------------------
|
|
563
|
+
|
|
564
|
+
/** The whole style, into the bound framebuffer, from a clear. */
|
|
565
|
+
private _scene(frame: RenderFrame): void {
|
|
566
|
+
const gl = this._gl;
|
|
567
|
+
const stats = this._stats;
|
|
568
|
+
const { width, height } = frame;
|
|
569
|
+
gl.viewport(0, 0, width, height);
|
|
570
|
+
const background = premultiplied(
|
|
571
|
+
this._color(frame.background ?? '#ffffff') ?? [1, 1, 1, 1],
|
|
572
|
+
);
|
|
573
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
574
|
+
gl.clearColor(background[0], background[1], background[2], background[3]);
|
|
575
|
+
gl.clearStencil(0);
|
|
576
|
+
gl.stencilMask(0xff);
|
|
577
|
+
gl.colorMask(true, true, true, true);
|
|
578
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
|
|
579
|
+
gl.disable(gl.DEPTH_TEST);
|
|
580
|
+
gl.disable(gl.CULL_FACE);
|
|
581
|
+
gl.enable(gl.BLEND);
|
|
582
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
583
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
584
|
+
|
|
585
|
+
for (const program of [this._line, this._fan]) {
|
|
586
|
+
gl.useProgram(program.program);
|
|
587
|
+
gl.uniform2f(program.uniforms.u_viewport, width, height);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
const edges = frame.edges ?? this._options.antialias;
|
|
591
|
+
const detail = frame.detail ?? 0;
|
|
592
|
+
const layers = frame.style.layers;
|
|
593
|
+
for (const tiles of frame.sources) {
|
|
594
|
+
// Upload before drawing, so a frame's uploads are counted once and a
|
|
595
|
+
// tile arriving mid-frame is not half drawn.
|
|
596
|
+
const scissors: (number[] | null)[] = [];
|
|
597
|
+
for (const tile of tiles) {
|
|
598
|
+
this._gpu(tile.data);
|
|
599
|
+
scissors.push(scissorOf(tile.clip, width, height));
|
|
600
|
+
}
|
|
601
|
+
stats.tiles += tiles.length;
|
|
602
|
+
// A raster source is its images, and no style.
|
|
603
|
+
if (tiles.some((tile) => tile.data.raster)) {
|
|
604
|
+
this._rasters(tiles, scissors, width, height);
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
for (let i = 0; i < layers.length; i++) {
|
|
608
|
+
const layer = layers[i].layer;
|
|
609
|
+
if (!drawsAt(layer, frame.zoom, detail)) continue;
|
|
610
|
+
if (layer.type === 'fill') {
|
|
611
|
+
if (this._fill(frame, tiles, i, layer, scissors, edges)) {
|
|
612
|
+
stats.layers++;
|
|
613
|
+
}
|
|
614
|
+
} else if (layer.type === 'line') {
|
|
615
|
+
if (this._lines(frame, tiles, i, layer, scissors)) stats.layers++;
|
|
616
|
+
} else if (layer.type === 'circle') {
|
|
617
|
+
if (this._circles(frame, tiles, i, layer, scissors)) stats.layers++;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
623
|
+
gl.disable(gl.STENCIL_TEST);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// --- layers ---------------------------------------------------------------
|
|
627
|
+
|
|
628
|
+
private _fill(
|
|
629
|
+
frame: RenderFrame,
|
|
630
|
+
tiles: readonly RenderTile[],
|
|
631
|
+
index: number,
|
|
632
|
+
layer: Extract<MapStyleLayer, { type: 'fill' }>,
|
|
633
|
+
scissors: (number[] | null)[],
|
|
634
|
+
edges: boolean,
|
|
635
|
+
): boolean {
|
|
636
|
+
const zoom = frame.zoom;
|
|
637
|
+
const opacity =
|
|
638
|
+
layer.opacity === undefined ? 1 : resolveZoomed(layer.opacity, zoom);
|
|
639
|
+
if (opacity <= 0) return false;
|
|
640
|
+
const base = this._color(resolveZoomed(layer.color, zoom));
|
|
641
|
+
if (!base) return false;
|
|
642
|
+
const outline =
|
|
643
|
+
layer.outlineColor === undefined
|
|
644
|
+
? undefined
|
|
645
|
+
: this._color(resolveZoomed(layer.outlineColor, zoom));
|
|
646
|
+
return this._fillWith(tiles, index, scissors, {
|
|
647
|
+
color: premultiplied(base, opacity),
|
|
648
|
+
// The edge: the outline colour a style asked for, at one logical
|
|
649
|
+
// pixel, or else the fill colour at half a device pixel — which is
|
|
650
|
+
// the antialiasing.
|
|
651
|
+
antialias: !outline && edges,
|
|
652
|
+
outline: outline ? premultiplied(outline, opacity) : null,
|
|
653
|
+
outlineHalf: Math.max(0.5, frame.scale / 2),
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* A polygon's ranges, over every tile that has them: the winding number
|
|
659
|
+
* into the stencil buffer, the colour wherever it is not zero, then the
|
|
660
|
+
* edge — the antialiasing, or an outline.
|
|
661
|
+
*/
|
|
662
|
+
private _fillWith(
|
|
663
|
+
tiles: readonly RenderTile[],
|
|
664
|
+
index: number,
|
|
665
|
+
scissors: (number[] | null)[],
|
|
666
|
+
paint: FillPaint,
|
|
667
|
+
): boolean {
|
|
668
|
+
const gl = this._gl;
|
|
669
|
+
// The cover's extent: the union of the squares this layer drew in.
|
|
670
|
+
const box = this._unionOf(tiles, index, scissors);
|
|
671
|
+
if (!box) return false;
|
|
672
|
+
const stats = this._stats;
|
|
673
|
+
|
|
674
|
+
// 1. The winding number, into the stencil buffer.
|
|
675
|
+
gl.useProgram(this._fan.program);
|
|
676
|
+
gl.colorMask(false, false, false, false);
|
|
677
|
+
gl.enable(gl.STENCIL_TEST);
|
|
678
|
+
gl.stencilFunc(gl.ALWAYS, 0, 0xff);
|
|
679
|
+
const fan = (count: number): void => {
|
|
680
|
+
gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, count);
|
|
681
|
+
};
|
|
682
|
+
const nonzero = this._options.fillRule === 'nonzero';
|
|
683
|
+
if (nonzero && this._separate) {
|
|
684
|
+
// Front faces count up and back faces down, in the one draw.
|
|
685
|
+
gl.disable(gl.CULL_FACE);
|
|
686
|
+
gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.INCR_WRAP);
|
|
687
|
+
gl.stencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.DECR_WRAP);
|
|
688
|
+
this._eachTile(tiles, index, scissors, this._fan, true, fan);
|
|
689
|
+
} else {
|
|
690
|
+
const passes: [number | null, number][] = nonzero
|
|
691
|
+
? [
|
|
692
|
+
[gl.BACK, gl.INCR_WRAP],
|
|
693
|
+
[gl.FRONT, gl.DECR_WRAP],
|
|
694
|
+
]
|
|
695
|
+
: [[null, gl.INVERT]];
|
|
696
|
+
for (const [cull, op] of passes) {
|
|
697
|
+
if (cull === null) gl.disable(gl.CULL_FACE);
|
|
698
|
+
else {
|
|
699
|
+
gl.enable(gl.CULL_FACE);
|
|
700
|
+
gl.cullFace(cull);
|
|
701
|
+
}
|
|
702
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, op);
|
|
703
|
+
this._eachTile(tiles, index, scissors, this._fan, true, fan);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
gl.disable(gl.CULL_FACE);
|
|
707
|
+
|
|
708
|
+
// 2. The colour, wherever the winding is not zero. An opaque fill sets
|
|
709
|
+
// the stencil back to zero as it goes, so the next layer starts clean;
|
|
710
|
+
// a translucent one keeps it for its edge, which is drawn outside the
|
|
711
|
+
// polygon only — laid over the fill's own colour it would be a darker
|
|
712
|
+
// rim round every translucent polygon — and clears it after.
|
|
713
|
+
const translucent = paint.color[3] < 1;
|
|
714
|
+
gl.colorMask(true, true, true, true);
|
|
715
|
+
gl.stencilFunc(gl.NOTEQUAL, 0, 0xff);
|
|
716
|
+
if (translucent) gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
717
|
+
else gl.stencilOp(gl.ZERO, gl.ZERO, gl.ZERO);
|
|
718
|
+
gl.scissor(box[0], box[1], box[2], box[3]);
|
|
719
|
+
this._drawCover(paint.color);
|
|
720
|
+
stats.drawCalls++;
|
|
721
|
+
|
|
722
|
+
// 3. The edge, which is the fill stream drawn as segments.
|
|
723
|
+
if (translucent) {
|
|
724
|
+
if (paint.antialias) {
|
|
725
|
+
// Where the winding is zero and no edge has been yet, each pixel
|
|
726
|
+
// once, at what a pixel the polygon partly covers is covered by:
|
|
727
|
+
// a segment of no width, whose ramp is half a pixel either side.
|
|
728
|
+
gl.stencilFunc(gl.EQUAL, 0, 0xff);
|
|
729
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
|
|
730
|
+
this._segments(
|
|
731
|
+
tiles,
|
|
732
|
+
index,
|
|
733
|
+
scissors,
|
|
734
|
+
{ color: paint.color, half: 0, dash: null },
|
|
735
|
+
true,
|
|
736
|
+
0,
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
this._clearStencil(box);
|
|
740
|
+
gl.disable(gl.STENCIL_TEST);
|
|
741
|
+
} else {
|
|
742
|
+
gl.disable(gl.STENCIL_TEST);
|
|
743
|
+
if (paint.antialias) {
|
|
744
|
+
this._strokeWith(
|
|
745
|
+
tiles,
|
|
746
|
+
index,
|
|
747
|
+
scissors,
|
|
748
|
+
{ color: paint.color, half: 0.5, dash: null },
|
|
749
|
+
true,
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
if (paint.outline) {
|
|
754
|
+
this._strokeWith(
|
|
755
|
+
tiles,
|
|
756
|
+
index,
|
|
757
|
+
scissors,
|
|
758
|
+
{ color: paint.outline, half: paint.outlineHalf, dash: null },
|
|
759
|
+
true,
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
return true;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
private _lines(
|
|
766
|
+
frame: RenderFrame,
|
|
767
|
+
tiles: readonly RenderTile[],
|
|
768
|
+
index: number,
|
|
769
|
+
layer: Extract<MapStyleLayer, { type: 'line' }>,
|
|
770
|
+
scissors: (number[] | null)[],
|
|
771
|
+
): boolean {
|
|
772
|
+
const zoom = frame.zoom;
|
|
773
|
+
const logical = resolveZoomed(layer.width, zoom);
|
|
774
|
+
if (!(logical > 0)) return false;
|
|
775
|
+
const opacity =
|
|
776
|
+
layer.opacity === undefined ? 1 : resolveZoomed(layer.opacity, zoom);
|
|
777
|
+
if (opacity <= 0) return false;
|
|
778
|
+
const base = this._color(resolveZoomed(layer.color, zoom));
|
|
779
|
+
if (!base) return false;
|
|
780
|
+
if (!this._unionOf(tiles, index, scissors)) return false;
|
|
781
|
+
// A road narrower than a pixel is drawn *at* a pixel, as paint.ts does:
|
|
782
|
+
// a motorway network that vanishes at zoom 6 is worse than a heavy one.
|
|
783
|
+
const width = Math.max(1, logical * frame.scale);
|
|
784
|
+
this._strokeWith(
|
|
785
|
+
tiles,
|
|
786
|
+
index,
|
|
787
|
+
scissors,
|
|
788
|
+
{
|
|
789
|
+
color: premultiplied(base, opacity),
|
|
790
|
+
half: width / 2,
|
|
791
|
+
dash: dashPattern(layer.dash, frame.scale),
|
|
792
|
+
},
|
|
793
|
+
false,
|
|
794
|
+
);
|
|
795
|
+
return true;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* A circle layer: a disc per point, one instanced draw per tile — the
|
|
800
|
+
* radius, colours and stroke `paint.ts` reads, at this zoom.
|
|
801
|
+
*/
|
|
802
|
+
private _circles(
|
|
803
|
+
frame: RenderFrame,
|
|
804
|
+
tiles: readonly RenderTile[],
|
|
805
|
+
index: number,
|
|
806
|
+
layer: Extract<MapStyleLayer, { type: 'circle' }>,
|
|
807
|
+
scissors: (number[] | null)[],
|
|
808
|
+
): boolean {
|
|
809
|
+
const zoom = frame.zoom;
|
|
810
|
+
const radius =
|
|
811
|
+
(layer.radius === undefined ? 3 : resolveZoomed(layer.radius, zoom)) *
|
|
812
|
+
frame.scale;
|
|
813
|
+
if (!(radius > 0)) return false;
|
|
814
|
+
const opacity =
|
|
815
|
+
layer.opacity === undefined ? 1 : resolveZoomed(layer.opacity, zoom);
|
|
816
|
+
if (opacity <= 0) return false;
|
|
817
|
+
const base = this._color(resolveZoomed(layer.color, zoom));
|
|
818
|
+
if (!base) return false;
|
|
819
|
+
if (!this._unionOf(tiles, index, scissors)) return false;
|
|
820
|
+
const ring =
|
|
821
|
+
layer.strokeColor === undefined
|
|
822
|
+
? null
|
|
823
|
+
: this._color(resolveZoomed(layer.strokeColor, zoom));
|
|
824
|
+
const stroke = ring
|
|
825
|
+
? (layer.strokeWidth === undefined
|
|
826
|
+
? 1
|
|
827
|
+
: resolveZoomed(layer.strokeWidth, zoom)) * frame.scale
|
|
828
|
+
: 0;
|
|
829
|
+
const gl = this._gl;
|
|
830
|
+
const program = (this._circle ??= linkProgram(
|
|
831
|
+
gl,
|
|
832
|
+
CIRCLE_VERTEX,
|
|
833
|
+
CIRCLE_FRAGMENT,
|
|
834
|
+
[
|
|
835
|
+
'u_tile',
|
|
836
|
+
'u_viewport',
|
|
837
|
+
'u_radius',
|
|
838
|
+
'u_stroke',
|
|
839
|
+
'u_color',
|
|
840
|
+
'u_stroke_color',
|
|
841
|
+
],
|
|
842
|
+
));
|
|
843
|
+
const u = program.uniforms;
|
|
844
|
+
const color = premultiplied(base, opacity);
|
|
845
|
+
const edge = ring ? premultiplied(ring, opacity) : CLEAR;
|
|
846
|
+
gl.useProgram(program.program);
|
|
847
|
+
gl.uniform2f(u.u_viewport, frame.width, frame.height);
|
|
848
|
+
gl.uniform1f(u.u_radius, radius);
|
|
849
|
+
gl.uniform1f(u.u_stroke, stroke);
|
|
850
|
+
gl.uniform4f(u.u_color, color[0], color[1], color[2], color[3]);
|
|
851
|
+
gl.uniform4f(u.u_stroke_color, edge[0], edge[1], edge[2], edge[3]);
|
|
852
|
+
this._eachTile(tiles, index, scissors, program, false, (count) => {
|
|
853
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, count);
|
|
854
|
+
});
|
|
855
|
+
return true;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* A raster source's tiles: each one's image over its square, clipped to
|
|
860
|
+
* the part of the view it draws. Past the source's depth the square is
|
|
861
|
+
* the data tile's, larger than the view's cells, so the image is drawn
|
|
862
|
+
* whole and stretched — the retained renderer's rule since #98.
|
|
863
|
+
*/
|
|
864
|
+
private _rasters(
|
|
865
|
+
tiles: readonly RenderTile[],
|
|
866
|
+
scissors: (number[] | null)[],
|
|
867
|
+
width: number,
|
|
868
|
+
height: number,
|
|
869
|
+
): void {
|
|
870
|
+
const gl = this._gl;
|
|
871
|
+
const program = (this._raster ??= linkProgram(
|
|
872
|
+
gl,
|
|
873
|
+
RASTER_VERTEX,
|
|
874
|
+
RASTER_FRAGMENT,
|
|
875
|
+
['u_tile', 'u_viewport', 'u_image'],
|
|
876
|
+
));
|
|
877
|
+
const u = program.uniforms;
|
|
878
|
+
gl.useProgram(program.program);
|
|
879
|
+
gl.uniform2f(u.u_viewport, width, height);
|
|
880
|
+
gl.uniform1i(u.u_image, 0);
|
|
881
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
882
|
+
gl.bindVertexArray(this._coverVao);
|
|
883
|
+
gl.disable(gl.STENCIL_TEST);
|
|
884
|
+
gl.enable(gl.BLEND);
|
|
885
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
886
|
+
for (let t = 0; t < tiles.length; t++) {
|
|
887
|
+
const tile = tiles[t];
|
|
888
|
+
const s = scissors[t];
|
|
889
|
+
const texture = this._tiles.get(tile.data)?.texture;
|
|
890
|
+
if (!s || !texture) continue;
|
|
891
|
+
gl.scissor(s[0], s[1], s[2], s[3]);
|
|
892
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
893
|
+
gl.uniform3f(u.u_tile, tile.size, tile.x, tile.y);
|
|
894
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
895
|
+
this._stats.drawCalls++;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* A stroke of one layer's ranges — of the line stream, or of the fill
|
|
901
|
+
* stream as a polygon's outline. A translucent one is drawn each pixel
|
|
902
|
+
* once: two capsules overlap at every join, and colour laid down twice
|
|
903
|
+
* there is a darker bead at every vertex. So the pixels a capsule covers
|
|
904
|
+
* wholly go first, under the stencil, the first capsule to reach one
|
|
905
|
+
* taking it; then the antialiased fringe, where nothing has been yet.
|
|
906
|
+
*/
|
|
907
|
+
private _strokeWith(
|
|
908
|
+
tiles: readonly RenderTile[],
|
|
909
|
+
index: number,
|
|
910
|
+
scissors: (number[] | null)[],
|
|
911
|
+
paint: StrokePaint,
|
|
912
|
+
fillStream: boolean,
|
|
913
|
+
): void {
|
|
914
|
+
if (paint.color[3] >= 1) {
|
|
915
|
+
this._segments(tiles, index, scissors, paint, fillStream, 0);
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
const box = this._unionOf(tiles, index, scissors);
|
|
919
|
+
if (!box) return;
|
|
920
|
+
const gl = this._gl;
|
|
921
|
+
gl.enable(gl.STENCIL_TEST);
|
|
922
|
+
gl.stencilFunc(gl.EQUAL, 0, 0xff);
|
|
923
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
|
|
924
|
+
this._segments(tiles, index, scissors, paint, fillStream, 1);
|
|
925
|
+
this._segments(tiles, index, scissors, paint, fillStream, 2);
|
|
926
|
+
this._clearStencil(box);
|
|
927
|
+
gl.disable(gl.STENCIL_TEST);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/** One layer's ranges as capsules, over every tile that has them. `part`
|
|
931
|
+
* 1 draws only the pixels a capsule covers wholly, 2 only the rest, and
|
|
932
|
+
* 0 both. */
|
|
933
|
+
private _segments(
|
|
934
|
+
tiles: readonly RenderTile[],
|
|
935
|
+
index: number,
|
|
936
|
+
scissors: (number[] | null)[],
|
|
937
|
+
paint: StrokePaint,
|
|
938
|
+
fillStream: boolean,
|
|
939
|
+
part: number,
|
|
940
|
+
): void {
|
|
941
|
+
const gl = this._gl;
|
|
942
|
+
const u = this._line.uniforms;
|
|
943
|
+
const { color, dash } = paint;
|
|
944
|
+
gl.useProgram(this._line.program);
|
|
945
|
+
gl.uniform4f(u.u_color, color[0], color[1], color[2], color[3]);
|
|
946
|
+
gl.uniform1f(u.u_half, paint.half);
|
|
947
|
+
// Unused entries are zero, and a dash of no length draws nothing.
|
|
948
|
+
const d = (i: number): number => dash?.[i] ?? 0;
|
|
949
|
+
gl.uniform4f(u.u_dash_a, d(0), d(1), d(2), d(3));
|
|
950
|
+
gl.uniform4f(u.u_dash_b, d(4), d(5), d(6), d(7));
|
|
951
|
+
gl.uniform1f(u.u_period, dash ? dash.reduce((sum, v) => sum + v, 0) : 0);
|
|
952
|
+
gl.uniform1f(u.u_part, part);
|
|
953
|
+
this._eachTile(tiles, index, scissors, this._line, fillStream, (count) => {
|
|
954
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, count);
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/** The union of the squares a layer draws in, as a scissor box; null
|
|
959
|
+
* when no tile draws it. */
|
|
960
|
+
private _unionOf(
|
|
961
|
+
tiles: readonly RenderTile[],
|
|
962
|
+
index: number,
|
|
963
|
+
scissors: (number[] | null)[],
|
|
964
|
+
): number[] | null {
|
|
965
|
+
let x0 = Infinity;
|
|
966
|
+
let y0 = Infinity;
|
|
967
|
+
let x1 = -Infinity;
|
|
968
|
+
let y1 = -Infinity;
|
|
969
|
+
for (let t = 0; t < tiles.length; t++) {
|
|
970
|
+
const s = scissors[t];
|
|
971
|
+
if (!s || !tiles[t].data.draws[index]) continue;
|
|
972
|
+
if (s[0] < x0) x0 = s[0];
|
|
973
|
+
if (s[1] < y0) y0 = s[1];
|
|
974
|
+
if (s[0] + s[2] > x1) x1 = s[0] + s[2];
|
|
975
|
+
if (s[1] + s[3] > y1) y1 = s[1] + s[3];
|
|
976
|
+
}
|
|
977
|
+
return x0 === Infinity ? null : [x0, y0, x1 - x0, y1 - y0];
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/** The stencil back to zero over a box, the colour untouched. */
|
|
981
|
+
private _clearStencil(box: number[]): void {
|
|
982
|
+
const gl = this._gl;
|
|
983
|
+
gl.colorMask(false, false, false, false);
|
|
984
|
+
gl.stencilFunc(gl.ALWAYS, 0, 0xff);
|
|
985
|
+
gl.stencilOp(gl.ZERO, gl.ZERO, gl.ZERO);
|
|
986
|
+
gl.scissor(box[0], box[1], box[2], box[3]);
|
|
987
|
+
this._drawCover(CLEAR);
|
|
988
|
+
this._stats.drawCalls++;
|
|
989
|
+
gl.colorMask(true, true, true, true);
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
// --- overlays ---------------------------------------------------------------
|
|
993
|
+
|
|
994
|
+
/**
|
|
995
|
+
* The overlays, over the labels — the retained renderer's order — from
|
|
996
|
+
* their bucket, placed this frame, pass by pass: a fill as a style's fill
|
|
997
|
+
* layer is drawn, a stroke as its line layer is. Answers how many passes
|
|
998
|
+
* it drew.
|
|
999
|
+
*/
|
|
1000
|
+
private _overlays(draw: OverlayDraw, frame: RenderFrame): number {
|
|
1001
|
+
const gl = this._gl;
|
|
1002
|
+
const { width, height, scale } = frame;
|
|
1003
|
+
this._gpu(draw.data);
|
|
1004
|
+
const clip = { x: 0, y: 0, width, height };
|
|
1005
|
+
const tiles: RenderTile[] = [
|
|
1006
|
+
{
|
|
1007
|
+
data: draw.data,
|
|
1008
|
+
x: draw.x,
|
|
1009
|
+
y: draw.y,
|
|
1010
|
+
size: draw.unit * TILE_EXTENT,
|
|
1011
|
+
clip,
|
|
1012
|
+
},
|
|
1013
|
+
];
|
|
1014
|
+
const scissors = [scissorOf(clip, width, height)];
|
|
1015
|
+
gl.viewport(0, 0, width, height);
|
|
1016
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
1017
|
+
gl.disable(gl.STENCIL_TEST);
|
|
1018
|
+
gl.enable(gl.BLEND);
|
|
1019
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
1020
|
+
for (const program of [this._line, this._fan]) {
|
|
1021
|
+
gl.useProgram(program.program);
|
|
1022
|
+
gl.uniform2f(program.uniforms.u_viewport, width, height);
|
|
1023
|
+
}
|
|
1024
|
+
let drawn = 0;
|
|
1025
|
+
for (const pass of draw.passes) {
|
|
1026
|
+
const base = this._color(pass.color);
|
|
1027
|
+
if (!base) continue;
|
|
1028
|
+
const color = premultiplied(base, pass.opacity);
|
|
1029
|
+
if (pass.kind === 'fill') {
|
|
1030
|
+
this._fillWith(tiles, pass.index, scissors, {
|
|
1031
|
+
color,
|
|
1032
|
+
antialias: this._options.antialias,
|
|
1033
|
+
outline: null,
|
|
1034
|
+
outlineHalf: 0,
|
|
1035
|
+
});
|
|
1036
|
+
} else {
|
|
1037
|
+
this._strokeWith(
|
|
1038
|
+
tiles,
|
|
1039
|
+
pass.index,
|
|
1040
|
+
scissors,
|
|
1041
|
+
{
|
|
1042
|
+
color,
|
|
1043
|
+
half: (pass.width * scale) / 2,
|
|
1044
|
+
dash: dashPattern(pass.dash, scale),
|
|
1045
|
+
},
|
|
1046
|
+
pass.outline === true,
|
|
1047
|
+
);
|
|
1048
|
+
}
|
|
1049
|
+
drawn++;
|
|
1050
|
+
}
|
|
1051
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
1052
|
+
return drawn;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* Every tile's ranges for one layer: scissor, place, draw. The loop the
|
|
1057
|
+
* whole frame is made of, so it allocates nothing and sets only what
|
|
1058
|
+
* changes between tiles.
|
|
1059
|
+
*/
|
|
1060
|
+
private _eachTile(
|
|
1061
|
+
tiles: readonly RenderTile[],
|
|
1062
|
+
index: number,
|
|
1063
|
+
scissors: (number[] | null)[],
|
|
1064
|
+
program: Program,
|
|
1065
|
+
fillStream: boolean,
|
|
1066
|
+
draw: (instances: number) => void,
|
|
1067
|
+
): void {
|
|
1068
|
+
const gl = this._gl;
|
|
1069
|
+
const stats = this._stats;
|
|
1070
|
+
for (let t = 0; t < tiles.length; t++) {
|
|
1071
|
+
const tile = tiles[t];
|
|
1072
|
+
const layerDraw = tile.data.draws[index];
|
|
1073
|
+
const s = scissors[t];
|
|
1074
|
+
if (!layerDraw || !s) continue;
|
|
1075
|
+
const gpu = this._tiles.get(tile.data);
|
|
1076
|
+
if (!gpu) continue;
|
|
1077
|
+
gl.scissor(s[0], s[1], s[2], s[3]);
|
|
1078
|
+
gl.uniform3f(
|
|
1079
|
+
program.uniforms.u_tile,
|
|
1080
|
+
tile.size / TILE_EXTENT,
|
|
1081
|
+
tile.x,
|
|
1082
|
+
tile.y,
|
|
1083
|
+
);
|
|
1084
|
+
// A fill layer's edges come from the fill stream too; which stream a
|
|
1085
|
+
// range indexes is the draw's kind, not the program's.
|
|
1086
|
+
const stream = fillStream || layerDraw.kind === 'fill';
|
|
1087
|
+
const ranges = layerDraw.ranges;
|
|
1088
|
+
for (let r = 0; r < ranges.length; r += 2) {
|
|
1089
|
+
const instances = ranges[r + 1] - 1;
|
|
1090
|
+
if (instances <= 0) continue;
|
|
1091
|
+
gl.bindVertexArray(this._vao(gpu, stream, ranges[r]));
|
|
1092
|
+
draw(instances);
|
|
1093
|
+
stats.drawCalls++;
|
|
1094
|
+
stats.instances += instances;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
// --- labels -----------------------------------------------------------------
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* The attribution, over whatever is bound: its box — the cover program,
|
|
1103
|
+
* scissored to it — and then its text, one more label quad.
|
|
1104
|
+
*/
|
|
1105
|
+
private _attribution(
|
|
1106
|
+
draw: AttributionDraw,
|
|
1107
|
+
width: number,
|
|
1108
|
+
height: number,
|
|
1109
|
+
): void {
|
|
1110
|
+
const gl = this._gl;
|
|
1111
|
+
const box = scissorOf(draw.box, width, height);
|
|
1112
|
+
if (box) {
|
|
1113
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
1114
|
+
gl.scissor(box[0], box[1], box[2], box[3]);
|
|
1115
|
+
gl.disable(gl.STENCIL_TEST);
|
|
1116
|
+
gl.enable(gl.BLEND);
|
|
1117
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
1118
|
+
this._drawCover(draw.boxColor);
|
|
1119
|
+
this._stats.drawCalls++;
|
|
1120
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
1121
|
+
}
|
|
1122
|
+
this._labels(draw.text, width, height);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/**
|
|
1126
|
+
* A batch of labels, over whatever is bound: one instanced draw of the
|
|
1127
|
+
* batch's quads, after the atlas texture has what the batch draws.
|
|
1128
|
+
* Answers how many it drew.
|
|
1129
|
+
*/
|
|
1130
|
+
private _labels(batch: LabelBatch, width: number, height: number): number {
|
|
1131
|
+
const gl = this._gl;
|
|
1132
|
+
const atlas = batch.atlas;
|
|
1133
|
+
const program = (this._label ??= linkProgram(
|
|
1134
|
+
gl,
|
|
1135
|
+
LABEL_VERTEX,
|
|
1136
|
+
LABEL_FRAGMENT,
|
|
1137
|
+
['u_viewport', 'u_atlas', 'u_inset', 'u_image'],
|
|
1138
|
+
));
|
|
1139
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
1140
|
+
this._uploadAtlas(atlas);
|
|
1141
|
+
if (batch.count === 0) return 0;
|
|
1142
|
+
|
|
1143
|
+
if (!this._labelVao) {
|
|
1144
|
+
this._labelBuffer = gl.createBuffer();
|
|
1145
|
+
this._labelVao = gl.createVertexArray();
|
|
1146
|
+
gl.bindVertexArray(this._labelVao);
|
|
1147
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._corners);
|
|
1148
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_corner);
|
|
1149
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_corner, 2, gl.FLOAT, false, 0, 0);
|
|
1150
|
+
gl.vertexAttribDivisor(ATTRIBUTES.a_corner, 0);
|
|
1151
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._labelBuffer);
|
|
1152
|
+
const stride = LABEL_INSTANCE * 4;
|
|
1153
|
+
const fields: [number, number, number][] = [
|
|
1154
|
+
[ATTRIBUTES.a_anchor, 4, 0],
|
|
1155
|
+
[ATTRIBUTES.a_rect, 4, 16],
|
|
1156
|
+
[ATTRIBUTES.a_ink, 4, 32],
|
|
1157
|
+
[ATTRIBUTES.a_halo, 4, 48],
|
|
1158
|
+
[ATTRIBUTES.a_params, 2, 64],
|
|
1159
|
+
];
|
|
1160
|
+
for (const [location, size, offset] of fields) {
|
|
1161
|
+
gl.enableVertexAttribArray(location);
|
|
1162
|
+
gl.vertexAttribPointer(location, size, gl.FLOAT, false, stride, offset);
|
|
1163
|
+
gl.vertexAttribDivisor(location, 1);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
gl.bindVertexArray(this._labelVao);
|
|
1167
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._labelBuffer);
|
|
1168
|
+
gl.bufferData(
|
|
1169
|
+
gl.ARRAY_BUFFER,
|
|
1170
|
+
batch.instances.subarray(0, batch.count * LABEL_INSTANCE),
|
|
1171
|
+
gl.STREAM_DRAW,
|
|
1172
|
+
);
|
|
1173
|
+
|
|
1174
|
+
gl.viewport(0, 0, width, height);
|
|
1175
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
1176
|
+
gl.disable(gl.STENCIL_TEST);
|
|
1177
|
+
gl.enable(gl.BLEND);
|
|
1178
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
1179
|
+
gl.useProgram(program.program);
|
|
1180
|
+
const u = program.uniforms;
|
|
1181
|
+
gl.uniform2f(u.u_viewport, width, height);
|
|
1182
|
+
gl.uniform2f(u.u_atlas, atlas.size, atlas.size);
|
|
1183
|
+
gl.uniform1f(u.u_inset, quadInset(atlas.pad));
|
|
1184
|
+
gl.uniform1i(u.u_image, 0);
|
|
1185
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, batch.count);
|
|
1186
|
+
this._stats.drawCalls++;
|
|
1187
|
+
return batch.count;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
// --- markers ----------------------------------------------------------------
|
|
1191
|
+
|
|
1192
|
+
/**
|
|
1193
|
+
* The markers, over whatever is bound: one instanced draw of a quad each,
|
|
1194
|
+
* every pixel shaded by its distance to the marker's outline. Answers how
|
|
1195
|
+
* many it drew.
|
|
1196
|
+
*/
|
|
1197
|
+
private _markers(batch: MarkerBatch, width: number, height: number): number {
|
|
1198
|
+
if (batch.count === 0) return 0;
|
|
1199
|
+
const gl = this._gl;
|
|
1200
|
+
const program = (this._marker ??= linkProgram(
|
|
1201
|
+
gl,
|
|
1202
|
+
MARKER_VERTEX,
|
|
1203
|
+
MARKER_FRAGMENT,
|
|
1204
|
+
['u_viewport'],
|
|
1205
|
+
));
|
|
1206
|
+
if (!this._markerVao) {
|
|
1207
|
+
this._markerBuffer = gl.createBuffer();
|
|
1208
|
+
this._markerVao = gl.createVertexArray();
|
|
1209
|
+
gl.bindVertexArray(this._markerVao);
|
|
1210
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._corners);
|
|
1211
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_corner);
|
|
1212
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_corner, 2, gl.FLOAT, false, 0, 0);
|
|
1213
|
+
gl.vertexAttribDivisor(ATTRIBUTES.a_corner, 0);
|
|
1214
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._markerBuffer);
|
|
1215
|
+
const stride = MARKER_INSTANCE * 4;
|
|
1216
|
+
const fields: [number, number, number][] = [
|
|
1217
|
+
[ATTRIBUTES.a_anchor, 4, 0],
|
|
1218
|
+
[ATTRIBUTES.a_ink, 4, 16],
|
|
1219
|
+
[ATTRIBUTES.a_halo, 4, 32],
|
|
1220
|
+
[ATTRIBUTES.a_params, 2, 48],
|
|
1221
|
+
];
|
|
1222
|
+
for (const [location, size, offset] of fields) {
|
|
1223
|
+
gl.enableVertexAttribArray(location);
|
|
1224
|
+
gl.vertexAttribPointer(location, size, gl.FLOAT, false, stride, offset);
|
|
1225
|
+
gl.vertexAttribDivisor(location, 1);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
gl.bindVertexArray(this._markerVao);
|
|
1229
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._markerBuffer);
|
|
1230
|
+
gl.bufferData(
|
|
1231
|
+
gl.ARRAY_BUFFER,
|
|
1232
|
+
batch.instances.subarray(0, batch.count * MARKER_INSTANCE),
|
|
1233
|
+
gl.STREAM_DRAW,
|
|
1234
|
+
);
|
|
1235
|
+
gl.viewport(0, 0, width, height);
|
|
1236
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
1237
|
+
gl.disable(gl.STENCIL_TEST);
|
|
1238
|
+
gl.enable(gl.BLEND);
|
|
1239
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
1240
|
+
gl.useProgram(program.program);
|
|
1241
|
+
gl.uniform2f(program.uniforms.u_viewport, width, height);
|
|
1242
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, batch.count);
|
|
1243
|
+
this._stats.drawCalls++;
|
|
1244
|
+
return batch.count;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* Take what the atlas has waiting into the texture — a few dozen rasters
|
|
1249
|
+
* a frame at most, so a batch landing is spread over frames rather than
|
|
1250
|
+
* paid in one. A texture that is new, or another atlas's, starts over:
|
|
1251
|
+
* the atlas queues everything again. Leaves the texture bound.
|
|
1252
|
+
*
|
|
1253
|
+
* The rasters go up as the text engine read them back — straight RGBA,
|
|
1254
|
+
* white where the glyphs are — because the shader reads only alpha:
|
|
1255
|
+
* premultiplying them first was a pass over every pixel for nothing.
|
|
1256
|
+
*/
|
|
1257
|
+
private _uploadAtlas(atlas: LabelAtlas): void {
|
|
1258
|
+
const gl = this._gl;
|
|
1259
|
+
if (!this._atlasTexture || this._atlasOwner !== atlas) {
|
|
1260
|
+
if (this._atlasTexture) gl.deleteTexture(this._atlasTexture);
|
|
1261
|
+
this._atlasTexture = gl.createTexture();
|
|
1262
|
+
gl.bindTexture(gl.TEXTURE_2D, this._atlasTexture);
|
|
1263
|
+
// Left undefined: every texel a label samples is one its own upload
|
|
1264
|
+
// wrote, margin included.
|
|
1265
|
+
gl.texImage2D(
|
|
1266
|
+
gl.TEXTURE_2D,
|
|
1267
|
+
0,
|
|
1268
|
+
gl.RGBA,
|
|
1269
|
+
atlas.size,
|
|
1270
|
+
atlas.size,
|
|
1271
|
+
0,
|
|
1272
|
+
gl.RGBA,
|
|
1273
|
+
gl.UNSIGNED_BYTE,
|
|
1274
|
+
null,
|
|
1275
|
+
);
|
|
1276
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
1277
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
1278
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
1279
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
1280
|
+
this._atlasOwner = atlas;
|
|
1281
|
+
atlas.restart();
|
|
1282
|
+
} else {
|
|
1283
|
+
gl.bindTexture(gl.TEXTURE_2D, this._atlasTexture);
|
|
1284
|
+
}
|
|
1285
|
+
for (const entry of atlas.takeUploads(ATLAS_UPLOADS_PER_FRAME)) {
|
|
1286
|
+
gl.texSubImage2D(
|
|
1287
|
+
gl.TEXTURE_2D,
|
|
1288
|
+
0,
|
|
1289
|
+
entry.x,
|
|
1290
|
+
entry.y,
|
|
1291
|
+
entry.width,
|
|
1292
|
+
entry.height,
|
|
1293
|
+
gl.RGBA,
|
|
1294
|
+
gl.UNSIGNED_BYTE,
|
|
1295
|
+
entry.pixels,
|
|
1296
|
+
);
|
|
1297
|
+
entry.ready = true;
|
|
1298
|
+
this._stats.uploadBytes += entry.pixels.length;
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
// --- resources ------------------------------------------------------------
|
|
1303
|
+
|
|
1304
|
+
private _color(value: string): Rgba | null {
|
|
1305
|
+
let color = this._colors.get(value);
|
|
1306
|
+
if (color === undefined) {
|
|
1307
|
+
color = parseColor(value);
|
|
1308
|
+
this._colors.set(value, color);
|
|
1309
|
+
}
|
|
1310
|
+
return color;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
private _gpu(data: GlTileData): TileGpu {
|
|
1314
|
+
let gpu = this._tiles.get(data);
|
|
1315
|
+
if (gpu) return gpu;
|
|
1316
|
+
const gl = this._gl;
|
|
1317
|
+
gpu = { line: null, fill: null, vaos: new Map(), texture: null, bytes: 0 };
|
|
1318
|
+
if (data.lineRecords > 1) {
|
|
1319
|
+
gpu.line = gl.createBuffer();
|
|
1320
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gpu.line);
|
|
1321
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(data.line), gl.STATIC_DRAW);
|
|
1322
|
+
gpu.bytes += data.line.byteLength;
|
|
1323
|
+
}
|
|
1324
|
+
if (data.fillRecords > 1) {
|
|
1325
|
+
gpu.fill = gl.createBuffer();
|
|
1326
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gpu.fill);
|
|
1327
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(data.fill), gl.STATIC_DRAW);
|
|
1328
|
+
gpu.bytes += data.fill.byteLength;
|
|
1329
|
+
}
|
|
1330
|
+
if (data.raster) {
|
|
1331
|
+
const { width, height, pixels } = data.raster;
|
|
1332
|
+
gpu.texture = gl.createTexture();
|
|
1333
|
+
gl.bindTexture(gl.TEXTURE_2D, gpu.texture);
|
|
1334
|
+
gl.texImage2D(
|
|
1335
|
+
gl.TEXTURE_2D,
|
|
1336
|
+
0,
|
|
1337
|
+
gl.RGBA,
|
|
1338
|
+
width,
|
|
1339
|
+
height,
|
|
1340
|
+
0,
|
|
1341
|
+
gl.RGBA,
|
|
1342
|
+
gl.UNSIGNED_BYTE,
|
|
1343
|
+
pixels,
|
|
1344
|
+
);
|
|
1345
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
1346
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
1347
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
1348
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
1349
|
+
gpu.bytes += width * height * 4;
|
|
1350
|
+
}
|
|
1351
|
+
this._tiles.set(data, gpu);
|
|
1352
|
+
this.gpuBytes += gpu.bytes;
|
|
1353
|
+
this._stats.uploads++;
|
|
1354
|
+
this._stats.uploadBytes += gpu.bytes;
|
|
1355
|
+
return gpu;
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* The vertex array for a range: the corner quad per vertex, and records
|
|
1360
|
+
* `first`, `first + 1` (and the third attribute) per instance.
|
|
1361
|
+
*
|
|
1362
|
+
* One per range because instanced attributes ignore `first` — GL 4.1 and
|
|
1363
|
+
* ES 3.0 have no base instance — so the range's start is baked into the
|
|
1364
|
+
* attribute offsets instead. A tile has one range per style layer it
|
|
1365
|
+
* draws, so this is two dozen small objects per tile, made once.
|
|
1366
|
+
*/
|
|
1367
|
+
private _vao(gpu: TileGpu, fill: boolean, first: number): unknown {
|
|
1368
|
+
const key = first * 2 + (fill ? 1 : 0);
|
|
1369
|
+
let vao = gpu.vaos.get(key);
|
|
1370
|
+
if (vao) return vao;
|
|
1371
|
+
const gl = this._gl;
|
|
1372
|
+
vao = gl.createVertexArray();
|
|
1373
|
+
gl.bindVertexArray(vao);
|
|
1374
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this._corners);
|
|
1375
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_corner);
|
|
1376
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_corner, 2, gl.FLOAT, false, 0, 0);
|
|
1377
|
+
gl.vertexAttribDivisor(ATTRIBUTES.a_corner, 0);
|
|
1378
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, fill ? gpu.fill : gpu.line);
|
|
1379
|
+
const base = first * RECORD_BYTES;
|
|
1380
|
+
const S = RECORD_BYTES;
|
|
1381
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_p0);
|
|
1382
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_p0, 2, gl.SHORT, false, S, base);
|
|
1383
|
+
gl.vertexAttribDivisor(ATTRIBUTES.a_p0, 1);
|
|
1384
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_p1);
|
|
1385
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_p1, 2, gl.SHORT, false, S, base + S);
|
|
1386
|
+
gl.vertexAttribDivisor(ATTRIBUTES.a_p1, 1);
|
|
1387
|
+
gl.enableVertexAttribArray(ATTRIBUTES.a_x);
|
|
1388
|
+
if (fill) {
|
|
1389
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_x, 2, gl.SHORT, false, S, base + 4);
|
|
1390
|
+
} else {
|
|
1391
|
+
gl.vertexAttribPointer(ATTRIBUTES.a_x, 1, gl.FLOAT, false, S, base + 4);
|
|
1392
|
+
}
|
|
1393
|
+
gl.vertexAttribDivisor(ATTRIBUTES.a_x, 1);
|
|
1394
|
+
gpu.vaos.set(key, vao);
|
|
1395
|
+
return vao;
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
private _drawCover(color: Rgba): void {
|
|
1399
|
+
const gl = this._gl;
|
|
1400
|
+
gl.useProgram(this._cover.program);
|
|
1401
|
+
gl.uniform4f(
|
|
1402
|
+
this._cover.uniforms.u_color,
|
|
1403
|
+
color[0],
|
|
1404
|
+
color[1],
|
|
1405
|
+
color[2],
|
|
1406
|
+
color[3],
|
|
1407
|
+
);
|
|
1408
|
+
gl.bindVertexArray(this._coverVao);
|
|
1409
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
/**
|
|
1413
|
+
* Does the bound framebuffer have a stencil buffer? Asked by drawing
|
|
1414
|
+
* rather than by querying, because the query differs between the default
|
|
1415
|
+
* framebuffer of a core profile (where `STENCIL_BITS` is gone) and a
|
|
1416
|
+
* user one, and the Cocoa surface is the second while XQuartz's is the
|
|
1417
|
+
* first. Two pixels, one read-back, on the first frame only.
|
|
1418
|
+
*
|
|
1419
|
+
* Both halves are needed, and the first version had only the second: **a
|
|
1420
|
+
* framebuffer with no stencil buffer passes every stencil test**, so "write
|
|
1421
|
+
* 1, then draw where it is 1" draws either way and cannot tell the two
|
|
1422
|
+
* apart. XQuartz's surface — which has none — answered yes, the frame went
|
|
1423
|
+
* straight to it, and every fill's cover pass painted the whole view in
|
|
1424
|
+
* that layer's colour. So pixel 0 asks the question that only a real
|
|
1425
|
+
* buffer answers "no" to: a draw where a *cleared* stencil is not zero.
|
|
1426
|
+
*/
|
|
1427
|
+
private _probeStencil(): boolean {
|
|
1428
|
+
const gl = this._gl;
|
|
1429
|
+
const previousVao = gl.getParameter(VERTEX_ARRAY_BINDING);
|
|
1430
|
+
gl.disable(gl.BLEND);
|
|
1431
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
1432
|
+
gl.scissor(0, 0, 2, 1);
|
|
1433
|
+
gl.clearColor(0, 0, 0, 0);
|
|
1434
|
+
gl.clearStencil(0);
|
|
1435
|
+
gl.stencilMask(0xff);
|
|
1436
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
|
|
1437
|
+
gl.enable(gl.STENCIL_TEST);
|
|
1438
|
+
// Pixel 0: stencil is 0 everywhere, so NOTEQUAL 0 must draw nothing.
|
|
1439
|
+
gl.scissor(0, 0, 1, 1);
|
|
1440
|
+
gl.stencilFunc(gl.NOTEQUAL, 0, 0xff);
|
|
1441
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
1442
|
+
this._drawCover([1, 0, 1, 1]);
|
|
1443
|
+
// Pixel 1: write 1, then draw where it is 1 — which must draw.
|
|
1444
|
+
gl.scissor(1, 0, 1, 1);
|
|
1445
|
+
gl.stencilFunc(gl.ALWAYS, 1, 0xff);
|
|
1446
|
+
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
|
|
1447
|
+
gl.colorMask(false, false, false, false);
|
|
1448
|
+
this._drawCover([0, 0, 0, 0]);
|
|
1449
|
+
gl.colorMask(true, true, true, true);
|
|
1450
|
+
gl.stencilFunc(gl.EQUAL, 1, 0xff);
|
|
1451
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
1452
|
+
this._drawCover([1, 0, 1, 1]);
|
|
1453
|
+
gl.disable(gl.STENCIL_TEST);
|
|
1454
|
+
const pixels = new Uint8Array(8);
|
|
1455
|
+
gl.readPixels(0, 0, 2, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
1456
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
1457
|
+
gl.bindVertexArray(previousVao);
|
|
1458
|
+
const magenta = (at: number) =>
|
|
1459
|
+
pixels[at] > 200 && pixels[at + 1] < 60 && pixels[at + 2] > 200;
|
|
1460
|
+
return !magenta(0) && magenta(4);
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
/** An offscreen colour target with its own stencil, made or remade at
|
|
1464
|
+
* this size. Leaves its framebuffer bound. */
|
|
1465
|
+
private _target(role: string, width: number, height: number): Offscreen {
|
|
1466
|
+
const gl = this._gl;
|
|
1467
|
+
const existing = this._targets.get(role);
|
|
1468
|
+
if (existing && existing.width === width && existing.height === height) {
|
|
1469
|
+
return existing;
|
|
1470
|
+
}
|
|
1471
|
+
if (existing) this._dropTarget(existing);
|
|
1472
|
+
const texture = gl.createTexture();
|
|
1473
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
1474
|
+
gl.texImage2D(
|
|
1475
|
+
gl.TEXTURE_2D,
|
|
1476
|
+
0,
|
|
1477
|
+
gl.RGBA,
|
|
1478
|
+
width,
|
|
1479
|
+
height,
|
|
1480
|
+
0,
|
|
1481
|
+
gl.RGBA,
|
|
1482
|
+
gl.UNSIGNED_BYTE,
|
|
1483
|
+
null,
|
|
1484
|
+
);
|
|
1485
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
1486
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
1487
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
1488
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
1489
|
+
const depth = gl.createRenderbuffer();
|
|
1490
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, depth);
|
|
1491
|
+
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH24_STENCIL8, width, height);
|
|
1492
|
+
const fbo = gl.createFramebuffer();
|
|
1493
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
1494
|
+
gl.framebufferTexture2D(
|
|
1495
|
+
gl.FRAMEBUFFER,
|
|
1496
|
+
gl.COLOR_ATTACHMENT0,
|
|
1497
|
+
gl.TEXTURE_2D,
|
|
1498
|
+
texture,
|
|
1499
|
+
0,
|
|
1500
|
+
);
|
|
1501
|
+
gl.framebufferRenderbuffer(
|
|
1502
|
+
gl.FRAMEBUFFER,
|
|
1503
|
+
gl.DEPTH_ATTACHMENT,
|
|
1504
|
+
gl.RENDERBUFFER,
|
|
1505
|
+
depth,
|
|
1506
|
+
);
|
|
1507
|
+
gl.framebufferRenderbuffer(
|
|
1508
|
+
gl.FRAMEBUFFER,
|
|
1509
|
+
gl.STENCIL_ATTACHMENT,
|
|
1510
|
+
gl.RENDERBUFFER,
|
|
1511
|
+
depth,
|
|
1512
|
+
);
|
|
1513
|
+
const target = { fbo, texture, depth, width, height };
|
|
1514
|
+
this._targets.set(role, target);
|
|
1515
|
+
return target;
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
/** An offscreen target onto the bound framebuffer: a copy at `alpha` 1,
|
|
1519
|
+
* a cross-fade below it. */
|
|
1520
|
+
private _composite(
|
|
1521
|
+
target: Offscreen,
|
|
1522
|
+
width: number,
|
|
1523
|
+
height: number,
|
|
1524
|
+
alpha: number,
|
|
1525
|
+
): void {
|
|
1526
|
+
const gl = this._gl;
|
|
1527
|
+
this._blit ??= linkProgram(gl, BLIT_VERTEX, BLIT_FRAGMENT, [
|
|
1528
|
+
'u_image',
|
|
1529
|
+
'u_alpha',
|
|
1530
|
+
]);
|
|
1531
|
+
gl.viewport(0, 0, width, height);
|
|
1532
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
1533
|
+
gl.disable(gl.STENCIL_TEST);
|
|
1534
|
+
if (alpha >= 1) gl.disable(gl.BLEND);
|
|
1535
|
+
else {
|
|
1536
|
+
gl.enable(gl.BLEND);
|
|
1537
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
1538
|
+
}
|
|
1539
|
+
gl.useProgram(this._blit.program);
|
|
1540
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
1541
|
+
gl.bindTexture(gl.TEXTURE_2D, target.texture);
|
|
1542
|
+
gl.uniform1i(this._blit.uniforms.u_image, 0);
|
|
1543
|
+
gl.uniform1f(this._blit.uniforms.u_alpha, alpha);
|
|
1544
|
+
gl.bindVertexArray(this._coverVao);
|
|
1545
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
1546
|
+
this._stats.drawCalls++;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
private _dropTarget(target: Offscreen): void {
|
|
1550
|
+
const gl = this._gl;
|
|
1551
|
+
gl.deleteFramebuffer(target.fbo);
|
|
1552
|
+
gl.deleteRenderbuffer(target.depth);
|
|
1553
|
+
gl.deleteTexture(target.texture);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
/**
|
|
1558
|
+
* A clip rectangle as a GL scissor box: whole pixels, bottom-left origin,
|
|
1559
|
+
* clamped to the viewport, or null when nothing of it is visible.
|
|
1560
|
+
*
|
|
1561
|
+
* Rounded edge by edge rather than origin-and-size, so two tiles that share
|
|
1562
|
+
* an edge round it to the same pixel column and neither a gap nor a double
|
|
1563
|
+
* row can open between them at any fractional zoom.
|
|
1564
|
+
*/
|
|
1565
|
+
export function scissorOf(
|
|
1566
|
+
clip: DeviceRect,
|
|
1567
|
+
width: number,
|
|
1568
|
+
height: number,
|
|
1569
|
+
): number[] | null {
|
|
1570
|
+
const x0 = Math.max(0, Math.round(clip.x));
|
|
1571
|
+
const x1 = Math.min(width, Math.round(clip.x + clip.width));
|
|
1572
|
+
const y0 = Math.max(0, Math.round(clip.y));
|
|
1573
|
+
const y1 = Math.min(height, Math.round(clip.y + clip.height));
|
|
1574
|
+
if (x1 <= x0 || y1 <= y0) return null;
|
|
1575
|
+
return [x0, height - y1, x1 - x0, y1 - y0];
|
|
1576
|
+
}
|