[Feat] Slow first draws on Windows: let PathLayer `billboard` and the `project` module's projection mode compile out of the shader (≈3–4× faster D3D compile)

Open
#11,224 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript

Research direction

Start by reading the shader code for PathLayer and the project module, then trace how layer props and viewport types become shader defines. Implement compile-time selection for billboard and projection modes while preserving runtime switching between the two Web Mercator modes. Done means the reported shader compile paths are reduced without changing rendering behavior; the issue offers measurements and a workaround to compare against.

Written by the indexing model from the issue text.

Description

feature
Target Use Case

(Prepared by Claude)

Summary

On Windows, Chrome and Edge run WebGL through ANGLE on Direct3D 11, and ANGLE compiles each program's D3D vertex shader with Microsoft's fxc at the program's first draw (the HLSL depends on the draw's vertex input layout), synchronously on the GPU process's main thread. Every WebGL context in the tab shares that thread, so the whole page's WebGL stops drawing until the compile ends. With GoogleMapsOverlay (interleaved), Google's basemap freezes along with deck.

Most of that compile time is spent on code that cannot run. deck selects behaviour with runtime ifs on uniforms, and HLSL has no real function calls: everything is inlined, then optimised. PathLayer's vertex shader projects the previous, current and next point twice: once in the billboard branch and once in the other one. That makes six inlined copies of project_position, each carrying every projection mode (WEB_MERCATOR, GLOBE, WEB_MERCATOR_AUTO_OFFSET, IDENTITY) and coordinate system. A given layer in a given view only ever takes one path through it.

Making the branches that are fixed per layer or per view into compile-time defines cuts the compile ~3.5× in our measurements, with no change in behaviour.

Measurements

deck.gl 9.4.1, luma.gl 9.4.2, GoogleMapsOverlay interleaved, Windows 11, Chromium and Edge (ANGLE D3D11), NVIDIA RTX 4050 Laptop / Intel Iris Xe. Cold GPU shader cache (fresh browser profile). Browser timings come from a Chrome trace (gpu.angle category, D3DCompile events). fxc timings were taken by extracting the HLSL that ANGLE generated (it is in the trace event's source argument) and compiling it with fxc /T vs_5_0 /O1 from the Windows SDK, best of 3–5 runs.

A PathLayer (~720 paths, billboard: false, default coordinateSystem) switched on for the first time:

Vertex shader fxc D3DCompile in the browser
As shipped 573–589 ms 617–662 ms (one stall; the main thread idles, frames stop)
path.billboard → false 220–231 ms 366–424 ms
…and GLOBE / IDENTITY projection branches → false 170 ms 196–300 ms
…and coordinate system fixed to LNGLAT 156 ms —

The whole page load (about 40–60 D3DCompiles across all our layers, plus Google's), with the two folds applied to every layer: 1.6–2.8 s → 0.9–1.4 s of D3D compile in total; the largest single compile went from 625–1073 ms to 167–253 ms.

Chrome's on-disk GPU program cache hides this on later page loads, but every browser or driver update starts cold again, and so does every new user.

Proposal
  1. billboard as a define. PathLayer already compiles ANTIALIASING and DASH_ENABLED as defines chosen from props. Doing the same for billboard (#ifdef BILLBOARD … #else … #endif around the two branches) removes half of the PathLayer vertex shader. A prop change builds another program, which luma caches by source. The same if (x.billboard) pattern exists in ScatterplotLayer, IconLayer, MultiIconLayer (text) and TextBackgroundLayer. We have not measured those, but each projects one position rather than three, so the gain is probably smaller there.

  2. Projection modes that a view can never use, compiled out of project. The projection mode is a property of the viewport. WebMercatorViewport can only produce WEB_MERCATOR or WEB_MERCATOR_AUTO_OFFSET (switching at zoom 12), never GLOBE or IDENTITY. A per-viewport-class define (e.g. PROJECTION_WEB_MERCATOR_ONLY) that removes the GLOBE and IDENTITY branches keeps both mercator modes as runtime uniforms, so zooming across 12 still costs no recompile. The define changes only when the view type does.

  3. Maybe: coordinateSystem as a define. It is a layer prop and rarely changes. In our measurements it was worth a further ~10% (170 → 156 ms). It is smaller than the other two, and a prop change would build a new program, so this one is optional.

Other candidates of the same kind that we noticed but did not measure: extruded in SolidPolygonLayer and ColumnLayer, and filled / stroked in the ScatterplotLayer fragment shader.

Workaround we are using

Text substitutions on deck's shaders before luma assembles them, applied when the props make the branch dead:

import { project } from "@deck.gl/core";
import { PathLayer } from "@deck.gl/layers";

// Every project-module consumer: a WebMercatorViewport is never GLOBE or IDENTITY.
const p = project as { vs: string };
p.vs = p.vs
  .replaceAll("project.projectionMode == PROJECTION_MODE_GLOBE", "false")
  .replaceAll("project.projectionMode == PROJECTION_MODE_IDENTITY", "false")
  .replaceAll("project.projectionMode != PROJECTION_MODE_IDENTITY", "true");

// PathLayer, including the outlines PolygonLayer/GeoJsonLayer create.
const getShaders = PathLayer.prototype.getShaders;
PathLayer.prototype.getShaders = function () {
  const shaders = getShaders.call(this);
  return this.props.billboard ? shaders : { ...shaders, vs: shaders.vs.replaceAll("path.billboard", "false") };
};

ANGLE's translator drops the if (false) branch before it emits HLSL, so this gives the same result as deleting the branch (231 ms vs 247 ms in fxc). It works, but it depends on deck's GLSL text staying the same, which is why we would rather have it upstream. We are happy to open a PR for (1) and (2) if this direction is acceptable.

Related

  • #1126 and #936: older reports of slow first draws caused by the size of the path and fp64 shaders.
  • microsoft/angle#140: long shader compile times with ANGLE's D3D11 backend.
Dominant language
TypeScript
Stars
14.8k
Forks
2.3k
Avg merge
1d 9h
Merged PRs (30d)
230

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from visgl/deck.gl

All issues in visgl/deck.gl

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.