wallhammer-web 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,1157 @@
|
|
|
1
|
+
// The device: everything the modules above gpu may ask of the graphics card, without touching WebGL themselves.
|
|
2
|
+
// Draw state is cached, so setting the same state twice costs nothing.
|
|
3
|
+
//
|
|
4
|
+
// Depth. The device owns the depth convention of whatever target is bound, so nothing above it handles a depth mode
|
|
5
|
+
// (`DepthMode`, chosen once with `setDepthMode`):
|
|
6
|
+
// - A depth target (shadow map) always holds standard depth: 0 nearest, 1 farthest, clip z from −w to +w.
|
|
7
|
+
// - The main target in the standard mode is the canvas with its own depth buffer, exactly as without depth modes.
|
|
8
|
+
// - In the reversed and logarithmic modes the main target is an offscreen multisampled target with a 32-bit float
|
|
9
|
+
// depth buffer (a canvas cannot have one); `present` copies it to the canvas. Reversed: clip z from 0 to w (`EXT_clip_control`), 1 nearest,
|
|
10
|
+
// 0 farthest, so the device clears depth to 0, turns `'less'` into "greater" and flips the sign of the polygon offset.
|
|
11
|
+
// Logarithmic: standard convention, but programs write their own depth, which takes the pipeline's polygon offset
|
|
12
|
+
// out of play; the device hands the offset to the program instead (`Program.depthBias`).
|
|
13
|
+
// `DrawState` keeps one meaning in every mode: `'less'` passes what is nearer, a positive polygon offset pushes away.
|
|
14
|
+
// Samples (`setMainSamples`): with 2 samples the standard mode draws into the offscreen target too (standard depth
|
|
15
|
+
// convention, 32-bit float depth), as a canvas cannot be asked for a sample count.
|
|
16
|
+
import { texelBytes } from '../base/data.js';
|
|
17
|
+
import { Program, bindAttribs, createBuffer, createTexture, drawMesh, forgetContextExtensions, rendererName } from './gl.js';
|
|
18
|
+
/** Full-screen triangle without vertex data, built from `gl_VertexID`. */
|
|
19
|
+
const FULLSCREEN_VS = /* glsl */ `#version 300 es
|
|
20
|
+
void main() {
|
|
21
|
+
// One triangle across the whole target.
|
|
22
|
+
vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
|
|
23
|
+
gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
|
|
24
|
+
}`;
|
|
25
|
+
/** Copies the resolved main target to the canvas, pixel for pixel. */
|
|
26
|
+
const PRESENT_FS = /* glsl */ `#version 300 es
|
|
27
|
+
precision highp float;
|
|
28
|
+
precision highp sampler2D;
|
|
29
|
+
uniform sampler2D u_src;
|
|
30
|
+
out vec4 o_color;
|
|
31
|
+
void main() {
|
|
32
|
+
o_color = vec4(texelFetch(u_src, ivec2(gl_FragCoord.xy), 0).rgb, 1.0);
|
|
33
|
+
}`;
|
|
34
|
+
/** Samples per pixel of the offscreen main target (what a canvas with antialiasing usually has). */
|
|
35
|
+
const SCENE_SAMPLES = 4;
|
|
36
|
+
export class Device {
|
|
37
|
+
gl;
|
|
38
|
+
name;
|
|
39
|
+
emptyVao;
|
|
40
|
+
state = {
|
|
41
|
+
depthTest: false,
|
|
42
|
+
depthFunc: 'less',
|
|
43
|
+
depthWrite: true,
|
|
44
|
+
cull: 'none',
|
|
45
|
+
blend: null,
|
|
46
|
+
alphaToCoverage: false,
|
|
47
|
+
polygonOffset: null,
|
|
48
|
+
};
|
|
49
|
+
/** The face `gl.cullFace` names now; it is switched only when a draw culls the other one. */
|
|
50
|
+
cullSide = 'back';
|
|
51
|
+
depth = 'standard';
|
|
52
|
+
/** Samples of the offscreen main target, and whether the standard mode draws offscreen too (`setMainSamples`). */
|
|
53
|
+
sceneSamples = SCENE_SAMPLES;
|
|
54
|
+
standardOffscreen = false;
|
|
55
|
+
clip = null;
|
|
56
|
+
scene = null;
|
|
57
|
+
presentProgram = null;
|
|
58
|
+
/** Depth convention of the bound target: true = 1 nearest, 0 farthest (the main target in the reversed mode). */
|
|
59
|
+
reversedNow = false;
|
|
60
|
+
/** True while the bound program applies the polygon offset itself (the main target in the logarithmic mode). */
|
|
61
|
+
biasInProgram = false;
|
|
62
|
+
/** The program in use, as far as the device knows (null after code that uses a program behind its back). */
|
|
63
|
+
current = null;
|
|
64
|
+
/** The framebuffer `target` bound last (null: the canvas), so a bake in the middle of a pass can put it back. */
|
|
65
|
+
boundFbo = null;
|
|
66
|
+
/** Sampler uniforms with a fixed texture unit (`fixSampler`) and the programs that already have them set. */
|
|
67
|
+
fixedSamplers = {};
|
|
68
|
+
hasFixedSamplers = false;
|
|
69
|
+
samplersSet = new WeakSet();
|
|
70
|
+
/**
|
|
71
|
+
* Context loss (see `contextRestored`). Off by default. When on, the device keeps what every mesh, instance buffer
|
|
72
|
+
* and texture was made from (references to the caller's arrays, not copies; for a bake its inputs) and makes all of
|
|
73
|
+
* them again, in place, in the fresh context: holders of a `Mesh`, `InstanceBuffer` or `Texture` notice nothing.
|
|
74
|
+
* The price is memory: uploaded data stays reachable for as long as its GPU object lives. Set it before anything
|
|
75
|
+
* is uploaded; what was uploaded before is not kept.
|
|
76
|
+
*/
|
|
77
|
+
retainUploads = false;
|
|
78
|
+
programs = [];
|
|
79
|
+
blocks = [];
|
|
80
|
+
depthTargets = new Set();
|
|
81
|
+
colorTargets = new Set();
|
|
82
|
+
meshRecipes = new Map();
|
|
83
|
+
/** Vertex buffer → the mesh that owns it, for `writeBuffer`. */
|
|
84
|
+
vertexOwner = new Map();
|
|
85
|
+
instanceRecipes = new Map();
|
|
86
|
+
/** The handle a caller holds → how to make the texture again (not for baked ones, see `bakeOf`). */
|
|
87
|
+
textureRecipes = new Map();
|
|
88
|
+
bakeOf = new Map();
|
|
89
|
+
/** The handle a caller holds → the texture that stands behind it now (only once the two differ). */
|
|
90
|
+
liveTexture = new Map();
|
|
91
|
+
restoredListeners = [];
|
|
92
|
+
loseExt = null;
|
|
93
|
+
/** Bytes per live GPU object, so a delete subtracts what its create added. */
|
|
94
|
+
compilingPrograms = [];
|
|
95
|
+
allocated = new Map();
|
|
96
|
+
totals = { buffers: 0, textures: 0, targets: 0 };
|
|
97
|
+
constructor(gl) {
|
|
98
|
+
this.gl = gl;
|
|
99
|
+
this.name = rendererName(gl);
|
|
100
|
+
const vao = gl.createVertexArray();
|
|
101
|
+
if (!vao)
|
|
102
|
+
throw new Error('Cannot create a VAO');
|
|
103
|
+
this.emptyVao = vao;
|
|
104
|
+
gl.cullFace(gl.BACK);
|
|
105
|
+
this.cullSide = 'back';
|
|
106
|
+
this.setState({ ...this.state }, true);
|
|
107
|
+
}
|
|
108
|
+
/** The depth mode in effect (see `setDepthMode`). */
|
|
109
|
+
get depthMode() {
|
|
110
|
+
return this.depth;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Chooses how the main target stores depth. Call it once, before the first frame and before anything is drawn.
|
|
114
|
+
* Returns the mode in effect: `'reversed'` becomes `'logarithmic'` when the browser lacks `EXT_clip_control`.
|
|
115
|
+
* For the two modes other than `'standard'` create the context with `createContext(canvas, mode)`: the device
|
|
116
|
+
* brings its own multisampled target, so the canvas needs neither antialiasing nor a depth buffer.
|
|
117
|
+
*/
|
|
118
|
+
setDepthMode(requested) {
|
|
119
|
+
const gl = this.gl;
|
|
120
|
+
let mode = requested;
|
|
121
|
+
if (mode === 'reversed') {
|
|
122
|
+
this.clip ??= gl.getExtension('EXT_clip_control');
|
|
123
|
+
if (!this.clip)
|
|
124
|
+
mode = 'logarithmic';
|
|
125
|
+
}
|
|
126
|
+
if (mode === this.depth)
|
|
127
|
+
return mode;
|
|
128
|
+
this.depth = mode;
|
|
129
|
+
if (!this.offscreen())
|
|
130
|
+
this.dropScene();
|
|
131
|
+
// The convention flags describe the bound target; until the next `target` call that is the main one.
|
|
132
|
+
this.convention(true);
|
|
133
|
+
return mode;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Chooses the samples per pixel of the main target: 0 (no antialiasing), 2 or 4. Call it once, before the first
|
|
137
|
+
* frame, and create the context with the same count (`createContext(canvas, mode, samples)`). Without this call
|
|
138
|
+
* nothing changes: the canvas's own antialiasing in the standard mode, 4 samples in the others. In the standard
|
|
139
|
+
* mode 0 and 4 keep drawing into the canvas (made without or with antialiasing); 2 draws into an offscreen target
|
|
140
|
+
* as the other modes do, as a canvas cannot be asked for a sample count.
|
|
141
|
+
*/
|
|
142
|
+
setMainSamples(samples) {
|
|
143
|
+
if (samples === this.sceneSamples && this.standardOffscreen === (samples === 2))
|
|
144
|
+
return;
|
|
145
|
+
this.dropScene();
|
|
146
|
+
this.sceneSamples = samples;
|
|
147
|
+
this.standardOffscreen = samples === 2;
|
|
148
|
+
}
|
|
149
|
+
/** True when the main target is the device's offscreen target rather than the canvas. */
|
|
150
|
+
offscreen() {
|
|
151
|
+
return this.depth !== 'standard' || this.standardOffscreen;
|
|
152
|
+
}
|
|
153
|
+
/** Switches clip range, depth comparison and polygon offset to the convention of the target just bound. */
|
|
154
|
+
convention(main) {
|
|
155
|
+
const reversed = main && this.depth === 'reversed';
|
|
156
|
+
const biasInProgram = main && this.depth === 'logarithmic';
|
|
157
|
+
if (reversed === this.reversedNow && biasInProgram === this.biasInProgram)
|
|
158
|
+
return;
|
|
159
|
+
this.reversedNow = reversed;
|
|
160
|
+
this.biasInProgram = biasInProgram;
|
|
161
|
+
const clip = this.clip;
|
|
162
|
+
if (clip)
|
|
163
|
+
clip.clipControlEXT(clip.LOWER_LEFT_EXT, reversed ? clip.ZERO_TO_ONE_EXT : clip.NEGATIVE_ONE_TO_ONE_EXT);
|
|
164
|
+
// The cached draw state keeps its meaning; what it maps to in WebGL has just changed.
|
|
165
|
+
this.gl.depthFunc(this.depthFunc(this.state.depthFunc));
|
|
166
|
+
this.applyPolygonOffset(this.state.polygonOffset);
|
|
167
|
+
}
|
|
168
|
+
/** WebGL comparison for "nearer" (`'less'`) or "nearer or equal" (`'lequal'`) in the bound target's convention. */
|
|
169
|
+
depthFunc(f) {
|
|
170
|
+
const gl = this.gl;
|
|
171
|
+
if (this.reversedNow)
|
|
172
|
+
return f === 'less' ? gl.GREATER : gl.GEQUAL;
|
|
173
|
+
return f === 'less' ? gl.LESS : gl.LEQUAL;
|
|
174
|
+
}
|
|
175
|
+
/** Polygon offset in the bound target's convention: positive always pushes away from the viewer. */
|
|
176
|
+
applyPolygonOffset(o) {
|
|
177
|
+
const gl = this.gl;
|
|
178
|
+
// A program that writes its own depth takes the offset as a uniform right before the draw (`syncDepthBias`).
|
|
179
|
+
if (o && !this.biasInProgram) {
|
|
180
|
+
gl.enable(gl.POLYGON_OFFSET_FILL);
|
|
181
|
+
if (this.reversedNow)
|
|
182
|
+
gl.polygonOffset(-o[0], -o[1]);
|
|
183
|
+
else
|
|
184
|
+
gl.polygonOffset(o[0], o[1]);
|
|
185
|
+
}
|
|
186
|
+
else
|
|
187
|
+
gl.disable(gl.POLYGON_OFFSET_FILL);
|
|
188
|
+
}
|
|
189
|
+
/** Hands the polygon offset of the draw state to the program in use, for programs that write their own depth. */
|
|
190
|
+
syncDepthBias() {
|
|
191
|
+
const p = this.current;
|
|
192
|
+
if (!p)
|
|
193
|
+
return;
|
|
194
|
+
const o = this.state.polygonOffset;
|
|
195
|
+
p.depthBias(o ? o[0] : 0, o ? o[1] : 0);
|
|
196
|
+
}
|
|
197
|
+
/** The offscreen main target, (re)allocated to the size of the drawing surface. */
|
|
198
|
+
sceneTarget() {
|
|
199
|
+
const gl = this.gl;
|
|
200
|
+
const width = gl.drawingBufferWidth;
|
|
201
|
+
const height = gl.drawingBufferHeight;
|
|
202
|
+
if (this.scene && this.scene.width === width && this.scene.height === height)
|
|
203
|
+
return this.scene;
|
|
204
|
+
this.dropScene();
|
|
205
|
+
const fbo = gl.createFramebuffer();
|
|
206
|
+
const resolveFbo = gl.createFramebuffer();
|
|
207
|
+
const color = gl.createRenderbuffer();
|
|
208
|
+
const depth = gl.createRenderbuffer();
|
|
209
|
+
if (!fbo || !resolveFbo || !color || !depth)
|
|
210
|
+
throw new Error('Cannot create the offscreen main target');
|
|
211
|
+
const samples = Math.min(this.sceneSamples, gl.getParameter(gl.MAX_SAMPLES));
|
|
212
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, color);
|
|
213
|
+
gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, gl.RGBA8, width, height);
|
|
214
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, depth);
|
|
215
|
+
gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, gl.DEPTH_COMPONENT32F, width, height);
|
|
216
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
|
|
217
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
218
|
+
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, color);
|
|
219
|
+
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depth);
|
|
220
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
|
|
221
|
+
throw new Error('The offscreen main target is incomplete');
|
|
222
|
+
// The texture is created on the active unit; the renderer and the layers bind what they need before every draw.
|
|
223
|
+
const resolved = createTexture(gl, { internalFormat: gl.RGBA8, format: gl.RGBA, type: gl.UNSIGNED_BYTE, width, height });
|
|
224
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
225
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, resolveFbo);
|
|
226
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, resolved, 0);
|
|
227
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
|
|
228
|
+
throw new Error('The resolve target is incomplete');
|
|
229
|
+
const s = { fbo, color, depth, resolveFbo, resolved, width, height };
|
|
230
|
+
// Four bytes of colour and four of depth per sample, and the resolved colour.
|
|
231
|
+
this.track(s, 'targets', width * height * (Math.max(1, samples) * 8 + 4));
|
|
232
|
+
this.presentProgram ??= this.program('present', FULLSCREEN_VS, PRESENT_FS);
|
|
233
|
+
this.scene = s;
|
|
234
|
+
return s;
|
|
235
|
+
}
|
|
236
|
+
dropScene() {
|
|
237
|
+
const s = this.scene;
|
|
238
|
+
if (!s)
|
|
239
|
+
return;
|
|
240
|
+
const gl = this.gl;
|
|
241
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
242
|
+
this.boundFbo = null;
|
|
243
|
+
gl.deleteFramebuffer(s.fbo);
|
|
244
|
+
gl.deleteFramebuffer(s.resolveFbo);
|
|
245
|
+
gl.deleteRenderbuffer(s.color);
|
|
246
|
+
gl.deleteRenderbuffer(s.depth);
|
|
247
|
+
gl.deleteTexture(s.resolved);
|
|
248
|
+
this.untrack(s);
|
|
249
|
+
this.scene = null;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Shows the frame. In the reversed and logarithmic depth modes this resolves the samples of the offscreen main
|
|
253
|
+
* target and copies the result to the canvas; in the standard mode the canvas was drawn directly and nothing
|
|
254
|
+
* happens. Afterwards the canvas is the bound framebuffer in every mode, so `finish` reads from it. The copy changes
|
|
255
|
+
* the viewport, the draw state and the texture on unit 0; the next frame sets them again.
|
|
256
|
+
*/
|
|
257
|
+
present() {
|
|
258
|
+
const s = this.scene;
|
|
259
|
+
const program = this.presentProgram;
|
|
260
|
+
if (!s || !program || !this.offscreen())
|
|
261
|
+
return;
|
|
262
|
+
const gl = this.gl;
|
|
263
|
+
// Samples → one colour per pixel. Same size and same format on both sides, as a multisample resolve requires.
|
|
264
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, s.fbo);
|
|
265
|
+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, s.resolveFbo);
|
|
266
|
+
gl.blitFramebuffer(0, 0, s.width, s.height, 0, 0, s.width, s.height, gl.COLOR_BUFFER_BIT, gl.NEAREST);
|
|
267
|
+
// Resolved colour → canvas, as a plain draw: valid whatever format and antialiasing the canvas has.
|
|
268
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
269
|
+
this.boundFbo = null;
|
|
270
|
+
gl.viewport(0, 0, s.width, s.height);
|
|
271
|
+
this.setState({ depthTest: false, cullBack: false, blend: null, alphaToCoverage: false, polygonOffset: null });
|
|
272
|
+
this.use(program);
|
|
273
|
+
this.texture(0, s.resolved);
|
|
274
|
+
program.int('u_src', 0);
|
|
275
|
+
this.drawFullscreen();
|
|
276
|
+
this.texture(0, null);
|
|
277
|
+
gl.bindVertexArray(null);
|
|
278
|
+
}
|
|
279
|
+
/** True while the graphics context is lost: nothing can be drawn or uploaded until it is restored. */
|
|
280
|
+
get lost() {
|
|
281
|
+
return this.gl.isContextLost();
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Registers a function called at the end of `contextRestored`, when the device's own objects exist again. For
|
|
285
|
+
* owners of GPU objects the device could not make again itself (everything uploaded without `retainUploads`) and
|
|
286
|
+
* for whatever depends on GPU contents that are gone (a rendered shadow map). Returns a function that unregisters.
|
|
287
|
+
*/
|
|
288
|
+
onRestored(fn) {
|
|
289
|
+
this.restoredListeners.push(fn);
|
|
290
|
+
return () => {
|
|
291
|
+
const i = this.restoredListeners.indexOf(fn);
|
|
292
|
+
if (i >= 0)
|
|
293
|
+
this.restoredListeners.splice(i, 1);
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* The browser handed back a fresh context after a loss (the platform's `watchContextLoss` reports it): every object
|
|
298
|
+
* of the old one is void. The device puts the context state back and makes again, in place, what it knows how to
|
|
299
|
+
* make: every program (same `Program` objects), uniform block and depth target, and with `retainUploads` every
|
|
300
|
+
* mesh, instance buffer, texture and bake. Then the `onRestored` listeners run. Contents drawn on the GPU (shadow
|
|
301
|
+
* maps) are not kept; whoever drew them draws them again.
|
|
302
|
+
*/
|
|
303
|
+
contextRestored() {
|
|
304
|
+
const gl = this.gl;
|
|
305
|
+
if (gl.isContextLost())
|
|
306
|
+
return;
|
|
307
|
+
forgetContextExtensions(gl);
|
|
308
|
+
this.loseExt = null;
|
|
309
|
+
const vao = gl.createVertexArray();
|
|
310
|
+
if (!vao)
|
|
311
|
+
throw new Error('Cannot create a VAO');
|
|
312
|
+
this.emptyVao = vao;
|
|
313
|
+
gl.cullFace(gl.BACK);
|
|
314
|
+
this.cullSide = 'back';
|
|
315
|
+
// The offscreen main target is gone with the context; `target` allocates it again.
|
|
316
|
+
if (this.scene) {
|
|
317
|
+
this.untrack(this.scene);
|
|
318
|
+
this.scene = null;
|
|
319
|
+
}
|
|
320
|
+
// The old context took every object with it. What is made again below is counted again there; what nobody can
|
|
321
|
+
// make again (uploaded without `retainUploads`) must leave the memory count, or `memory()` would report it for
|
|
322
|
+
// ever. A texture that is made again keeps its entry: it is counted under the handle its holder has.
|
|
323
|
+
for (const [obj, a] of this.allocated) {
|
|
324
|
+
if (a.kind === 'textures' && (this.textureRecipes.has(obj) || this.bakeOf.has(obj)))
|
|
325
|
+
continue;
|
|
326
|
+
this.totals[a.kind] -= a.bytes;
|
|
327
|
+
this.allocated.delete(obj);
|
|
328
|
+
}
|
|
329
|
+
this.boundFbo = null;
|
|
330
|
+
this.current = null;
|
|
331
|
+
this.samplersSet = new WeakSet();
|
|
332
|
+
this.clip = this.depth === 'reversed' ? gl.getExtension('EXT_clip_control') : null;
|
|
333
|
+
// A fresh context has the standard convention; the next `target` call switches to the depth mode's own.
|
|
334
|
+
this.reversedNow = false;
|
|
335
|
+
this.biasInProgram = false;
|
|
336
|
+
this.setState({ ...this.state }, true);
|
|
337
|
+
for (const p of this.programs)
|
|
338
|
+
p.restore();
|
|
339
|
+
this.compilingPrograms = [...this.programs];
|
|
340
|
+
for (const fill of this.blocks)
|
|
341
|
+
fill();
|
|
342
|
+
for (const t of this.depthTargets) {
|
|
343
|
+
if (t.texture)
|
|
344
|
+
this.untrack(t.texture);
|
|
345
|
+
Object.assign(t, this.makeDepthTarget(t.size, t.repeat));
|
|
346
|
+
}
|
|
347
|
+
for (const t of this.colorTargets) {
|
|
348
|
+
this.untrack(t);
|
|
349
|
+
const made = this.makeColorTarget(t.width, t.height, t.textures.length, t.format);
|
|
350
|
+
// A fresh context that cannot hold the format any more leaves the target without textures; fills are skipped.
|
|
351
|
+
Object.assign(t, made ?? { fbo: null, textures: t.textures.map(() => null) });
|
|
352
|
+
if (made)
|
|
353
|
+
this.track(t, 'targets', colorTargetBytes(t));
|
|
354
|
+
}
|
|
355
|
+
for (const [b, data] of this.instanceRecipes) {
|
|
356
|
+
if (b.buffer)
|
|
357
|
+
this.untrack(b.buffer);
|
|
358
|
+
const buffer = createBuffer(gl, gl.ARRAY_BUFFER, data.data, gl.DYNAMIC_DRAW);
|
|
359
|
+
this.track(buffer, 'buffers', data.data.byteLength);
|
|
360
|
+
b.buffer = buffer;
|
|
361
|
+
}
|
|
362
|
+
// In the order they were made: a mesh that shares buffers comes after the mesh that owns them.
|
|
363
|
+
for (const [m, r] of this.meshRecipes) {
|
|
364
|
+
for (const b of m.buffers ?? []) {
|
|
365
|
+
this.untrack(b);
|
|
366
|
+
this.vertexOwner.delete(b);
|
|
367
|
+
}
|
|
368
|
+
const fresh = r.data ? this.buildMesh(r.data, r.locations, r.instances) : r.base ? this.buildShared(r.base, r.locations, r.instances) : null;
|
|
369
|
+
if (!fresh)
|
|
370
|
+
continue;
|
|
371
|
+
// Meshes that share this one's buffers hold its `source` object: it is refilled, not replaced.
|
|
372
|
+
const source = m.source;
|
|
373
|
+
Object.assign(m, fresh);
|
|
374
|
+
if (r.data && source && fresh.source) {
|
|
375
|
+
Object.assign(source, fresh.source);
|
|
376
|
+
m.source = source;
|
|
377
|
+
}
|
|
378
|
+
const vb = r.data ? m.buffers?.[0] : undefined;
|
|
379
|
+
if (!vb)
|
|
380
|
+
continue;
|
|
381
|
+
this.vertexOwner.set(vb, m);
|
|
382
|
+
if (r.patches) {
|
|
383
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, vb);
|
|
384
|
+
for (const [offset, bytes] of r.patches)
|
|
385
|
+
gl.bufferSubData(gl.ARRAY_BUFFER, offset, bytes);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (const [key, make] of this.textureRecipes)
|
|
389
|
+
this.liveTexture.set(key, make());
|
|
390
|
+
for (const recipe of new Set(this.bakeOf.values())) {
|
|
391
|
+
if (recipe.alive <= 0)
|
|
392
|
+
continue;
|
|
393
|
+
const made = recipe.run();
|
|
394
|
+
recipe.keys.forEach((key, i) => {
|
|
395
|
+
if (this.bakeOf.get(key) === recipe)
|
|
396
|
+
this.liveTexture.set(key, made[i]);
|
|
397
|
+
else
|
|
398
|
+
gl.deleteTexture(made[i]);
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
gl.bindVertexArray(null);
|
|
402
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
403
|
+
for (const fn of [...this.restoredListeners])
|
|
404
|
+
fn();
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Takes the context away on purpose (`WEBGL_lose_context`), as a driver reset would: for tests of the recovery.
|
|
408
|
+
* Returns false when the browser lacks the extension. `restoreContext` asks the browser to hand a fresh one back.
|
|
409
|
+
*/
|
|
410
|
+
loseContext() {
|
|
411
|
+
this.loseExt ??= this.gl.getExtension('WEBGL_lose_context');
|
|
412
|
+
if (!this.loseExt || this.gl.isContextLost())
|
|
413
|
+
return false;
|
|
414
|
+
this.loseExt.loseContext();
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
restoreContext() {
|
|
418
|
+
if (!this.loseExt || !this.gl.isContextLost())
|
|
419
|
+
return false;
|
|
420
|
+
this.loseExt.restoreContext();
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
/** GPU memory allocated through this device and not deleted, by kind. */
|
|
424
|
+
memory() {
|
|
425
|
+
const t = this.totals;
|
|
426
|
+
return { buffers: t.buffers, textures: t.textures, targets: t.targets, total: t.buffers + t.textures + t.targets };
|
|
427
|
+
}
|
|
428
|
+
track(obj, kind, bytes) {
|
|
429
|
+
this.allocated.set(obj, { kind, bytes });
|
|
430
|
+
this.totals[kind] += bytes;
|
|
431
|
+
}
|
|
432
|
+
untrack(obj) {
|
|
433
|
+
const a = this.allocated.get(obj);
|
|
434
|
+
if (!a)
|
|
435
|
+
return;
|
|
436
|
+
this.totals[a.kind] -= a.bytes;
|
|
437
|
+
this.allocated.delete(obj);
|
|
438
|
+
}
|
|
439
|
+
uniformBlock(binding, data, dynamic) {
|
|
440
|
+
const gl = this.gl;
|
|
441
|
+
let b = gl.createBuffer();
|
|
442
|
+
if (!b && !gl.isContextLost())
|
|
443
|
+
throw new Error('Cannot create a uniform buffer');
|
|
444
|
+
// The data last handed over (a reference), so the block can be filled again after a context loss.
|
|
445
|
+
let last = data;
|
|
446
|
+
const fill = () => {
|
|
447
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, b);
|
|
448
|
+
gl.bufferData(gl.UNIFORM_BUFFER, data.byteLength, dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW);
|
|
449
|
+
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, last);
|
|
450
|
+
gl.bindBufferBase(gl.UNIFORM_BUFFER, binding, b);
|
|
451
|
+
};
|
|
452
|
+
if (b) {
|
|
453
|
+
this.track(b, 'buffers', data.byteLength);
|
|
454
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, b);
|
|
455
|
+
gl.bufferData(gl.UNIFORM_BUFFER, data, dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW);
|
|
456
|
+
gl.bindBufferBase(gl.UNIFORM_BUFFER, binding, b);
|
|
457
|
+
}
|
|
458
|
+
this.blocks.push(() => {
|
|
459
|
+
if (b)
|
|
460
|
+
this.untrack(b);
|
|
461
|
+
b = gl.createBuffer();
|
|
462
|
+
if (!b)
|
|
463
|
+
return;
|
|
464
|
+
this.track(b, 'buffers', data.byteLength);
|
|
465
|
+
fill();
|
|
466
|
+
});
|
|
467
|
+
return {
|
|
468
|
+
floats: data.length,
|
|
469
|
+
update(next) {
|
|
470
|
+
last = next;
|
|
471
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, b);
|
|
472
|
+
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, next);
|
|
473
|
+
},
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
/** `repeat`: texture coordinates outside 0–1 wrap around instead of clamping to the edge (default off). */
|
|
477
|
+
depthTarget(size, repeat = false) {
|
|
478
|
+
if (this.gl.isContextLost()) {
|
|
479
|
+
// Made in the fresh context by `contextRestored`.
|
|
480
|
+
const t = { size, repeat, texture: null, fbo: null };
|
|
481
|
+
this.depthTargets.add(t);
|
|
482
|
+
return t;
|
|
483
|
+
}
|
|
484
|
+
const t = { size, repeat, ...this.makeDepthTarget(size, repeat) };
|
|
485
|
+
this.depthTargets.add(t);
|
|
486
|
+
return t;
|
|
487
|
+
}
|
|
488
|
+
/** The texture and framebuffer of a depth target. Read `DepthTarget.texture` when it is used: a restore replaces it. */
|
|
489
|
+
makeDepthTarget(size, repeat) {
|
|
490
|
+
const gl = this.gl;
|
|
491
|
+
const texture = createTexture(gl, {
|
|
492
|
+
internalFormat: gl.DEPTH_COMPONENT24,
|
|
493
|
+
format: gl.DEPTH_COMPONENT,
|
|
494
|
+
type: gl.UNSIGNED_INT,
|
|
495
|
+
width: size,
|
|
496
|
+
height: size,
|
|
497
|
+
minFilter: gl.LINEAR,
|
|
498
|
+
magFilter: gl.LINEAR,
|
|
499
|
+
...(repeat ? { wrap: gl.REPEAT } : {}),
|
|
500
|
+
});
|
|
501
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);
|
|
502
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_FUNC, gl.LEQUAL);
|
|
503
|
+
const fbo = gl.createFramebuffer();
|
|
504
|
+
if (!fbo)
|
|
505
|
+
throw new Error('Cannot create a depth target');
|
|
506
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
507
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, texture, 0);
|
|
508
|
+
gl.drawBuffers([gl.NONE]);
|
|
509
|
+
gl.readBuffer(gl.NONE);
|
|
510
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
|
|
511
|
+
throw new Error('Depth target is incomplete');
|
|
512
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
513
|
+
this.track(texture, 'targets', size * size * 4);
|
|
514
|
+
return { texture, fbo };
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* A colour target of `count` textures (one per output of the fragment shader that fills it). Returns null where
|
|
518
|
+
* the format cannot be drawn into: `'rgba16f'` needs `EXT_color_buffer_float` or `EXT_color_buffer_half_float`,
|
|
519
|
+
* and nothing can be made while the context is lost. The caller then does without the target.
|
|
520
|
+
*/
|
|
521
|
+
colorTarget(width, height, count, format) {
|
|
522
|
+
if (this.gl.isContextLost())
|
|
523
|
+
return null;
|
|
524
|
+
const made = this.makeColorTarget(width, height, count, format);
|
|
525
|
+
if (!made)
|
|
526
|
+
return null;
|
|
527
|
+
const t = { width, height, format, ...made };
|
|
528
|
+
this.colorTargets.add(t);
|
|
529
|
+
this.track(t, 'targets', colorTargetBytes(t));
|
|
530
|
+
return t;
|
|
531
|
+
}
|
|
532
|
+
/** The textures and framebuffer of a colour target, or null when the format cannot be drawn into. */
|
|
533
|
+
makeColorTarget(width, height, count, format) {
|
|
534
|
+
const gl = this.gl;
|
|
535
|
+
// Asking for the extension is what switches it on, also in a context made again after a loss.
|
|
536
|
+
if (format === 'rgba16f' && !gl.getExtension('EXT_color_buffer_float') && !gl.getExtension('EXT_color_buffer_half_float'))
|
|
537
|
+
return null;
|
|
538
|
+
const f = format === 'rgba16f' ? { internalFormat: gl.RGBA16F, format: gl.RGBA, type: gl.HALF_FLOAT } : { internalFormat: gl.RGBA8, format: gl.RGBA, type: gl.UNSIGNED_BYTE };
|
|
539
|
+
const textures = [];
|
|
540
|
+
for (let i = 0; i < count; i++)
|
|
541
|
+
textures.push(createTexture(gl, { ...f, width, height, minFilter: gl.LINEAR, magFilter: gl.LINEAR }));
|
|
542
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
543
|
+
const fbo = gl.createFramebuffer();
|
|
544
|
+
if (!fbo)
|
|
545
|
+
throw new Error('Cannot create a colour target');
|
|
546
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
547
|
+
textures.forEach((t, i) => gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, t, 0));
|
|
548
|
+
gl.drawBuffers(textures.map((_, i) => gl.COLOR_ATTACHMENT0 + i));
|
|
549
|
+
const complete = gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE;
|
|
550
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.boundFbo);
|
|
551
|
+
if (complete)
|
|
552
|
+
return { textures, fbo };
|
|
553
|
+
gl.deleteFramebuffer(fbo);
|
|
554
|
+
for (const t of textures)
|
|
555
|
+
gl.deleteTexture(t);
|
|
556
|
+
return null;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Draws one triangle across the whole of a colour target with `program`: its fragment shader has one output per
|
|
560
|
+
* texture of the target and writes every texel, so nothing is cleared. Afterwards the target bound before is
|
|
561
|
+
* bound again. The viewport and the draw state are changed: call it between passes (`Layer.prepare` of render),
|
|
562
|
+
* where the next pass sets both, not in the middle of one.
|
|
563
|
+
*/
|
|
564
|
+
fill(t, program, uniforms = {}) {
|
|
565
|
+
const fbo = t.fbo;
|
|
566
|
+
if (!fbo)
|
|
567
|
+
return;
|
|
568
|
+
const gl = this.gl;
|
|
569
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
570
|
+
gl.viewport(0, 0, t.width, t.height);
|
|
571
|
+
this.setState({ depthTest: false, depthWrite: false, cullBack: false, blend: null, alphaToCoverage: false, polygonOffset: null });
|
|
572
|
+
this.use(program);
|
|
573
|
+
for (const name in uniforms)
|
|
574
|
+
program.set(name, uniforms[name]);
|
|
575
|
+
gl.bindVertexArray(this.emptyVao);
|
|
576
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
577
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.boundFbo);
|
|
578
|
+
}
|
|
579
|
+
/** A 1 × 1 RGBA8 texture. */
|
|
580
|
+
pixelTexture(rgba) {
|
|
581
|
+
const gl = this.gl;
|
|
582
|
+
const make = () => createTexture(gl, { internalFormat: gl.RGBA8, format: gl.RGBA, type: gl.UNSIGNED_BYTE, width: 1, height: 1, data: new Uint8Array(rgba) });
|
|
583
|
+
const t = this.newTexture(make);
|
|
584
|
+
this.track(t, 'textures', 4);
|
|
585
|
+
return t;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* A texture made by `make`. With `retainUploads` the call is kept so the texture can be made again; while the
|
|
589
|
+
* context is lost the caller gets a handle with nothing behind it yet.
|
|
590
|
+
*/
|
|
591
|
+
newTexture(make) {
|
|
592
|
+
if (!this.retainUploads)
|
|
593
|
+
return make();
|
|
594
|
+
let t;
|
|
595
|
+
if (this.gl.isContextLost()) {
|
|
596
|
+
t = {};
|
|
597
|
+
this.liveTexture.set(t, null);
|
|
598
|
+
}
|
|
599
|
+
else
|
|
600
|
+
t = make();
|
|
601
|
+
this.textureRecipes.set(t, make);
|
|
602
|
+
return t;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Draws into a depth target, or into the main target with `null`: the canvas, or in the reversed and logarithmic
|
|
606
|
+
* depth modes the offscreen target that `present` copies to the canvas. Also switches the depth convention (clip
|
|
607
|
+
* range, comparison, polygon offset sign, clear value) to that of the target, see the top of this file.
|
|
608
|
+
*/
|
|
609
|
+
target(t) {
|
|
610
|
+
const gl = this.gl;
|
|
611
|
+
this.boundFbo = t ? t.fbo : this.offscreen() ? this.sceneTarget().fbo : null;
|
|
612
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.boundFbo);
|
|
613
|
+
this.convention(t === null);
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Copies the depth of the square `0…size` (pixels from the lower left corner) of one depth target into the same
|
|
617
|
+
* square of another of the same size. Nothing else of `to` changes. Afterwards the target bound before is bound again.
|
|
618
|
+
*/
|
|
619
|
+
copyDepth(from, to, size) {
|
|
620
|
+
const a = from.fbo;
|
|
621
|
+
const b = to.fbo;
|
|
622
|
+
if (!a || !b || size <= 0)
|
|
623
|
+
return;
|
|
624
|
+
const gl = this.gl;
|
|
625
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, a);
|
|
626
|
+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, b);
|
|
627
|
+
gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.DEPTH_BUFFER_BIT, gl.NEAREST);
|
|
628
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.boundFbo);
|
|
629
|
+
}
|
|
630
|
+
viewport(x, y, w, h) {
|
|
631
|
+
this.gl.viewport(x, y, w, h);
|
|
632
|
+
}
|
|
633
|
+
/** Clears colour (when given) and depth (to the farthest value of the bound target: 1, or 0 with reversed depth). */
|
|
634
|
+
clear(color, depth) {
|
|
635
|
+
const gl = this.gl;
|
|
636
|
+
let bits = 0;
|
|
637
|
+
if (color) {
|
|
638
|
+
gl.clearColor(color[0], color[1], color[2], 1);
|
|
639
|
+
bits |= gl.COLOR_BUFFER_BIT;
|
|
640
|
+
}
|
|
641
|
+
if (depth) {
|
|
642
|
+
gl.clearDepth(this.reversedNow ? 0 : 1);
|
|
643
|
+
bits |= gl.DEPTH_BUFFER_BIT;
|
|
644
|
+
}
|
|
645
|
+
gl.clear(bits);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Clears depth (to the farthest value of the bound target) inside one rectangle of the target, given in pixels
|
|
649
|
+
* from its lower left corner. Everything outside the rectangle is kept.
|
|
650
|
+
*/
|
|
651
|
+
clearDepthRect(x, y, w, h) {
|
|
652
|
+
const gl = this.gl;
|
|
653
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
654
|
+
gl.scissor(x, y, w, h);
|
|
655
|
+
gl.clearDepth(this.reversedNow ? 0 : 1);
|
|
656
|
+
gl.clear(gl.DEPTH_BUFFER_BIT);
|
|
657
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
658
|
+
}
|
|
659
|
+
texture(unit, tex) {
|
|
660
|
+
const gl = this.gl;
|
|
661
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
662
|
+
// After a context loss a handle may stand for a texture made again (`retainUploads`).
|
|
663
|
+
if (tex && this.liveTexture.size > 0) {
|
|
664
|
+
const live = this.liveTexture.get(tex);
|
|
665
|
+
if (live !== undefined) {
|
|
666
|
+
gl.bindTexture(gl.TEXTURE_2D, live);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Applies the given fields. `force` applies them even when the cache says they are set already: needed after code
|
|
674
|
+
* outside the device (a texture bake) may have changed the context state.
|
|
675
|
+
*/
|
|
676
|
+
setState(s, force = false) {
|
|
677
|
+
const gl = this.gl;
|
|
678
|
+
const c = this.state;
|
|
679
|
+
if (force) {
|
|
680
|
+
// Flip the cached copy of every given field, so each comparison below fails and the field is applied.
|
|
681
|
+
if (s.depthTest !== undefined)
|
|
682
|
+
c.depthTest = !s.depthTest;
|
|
683
|
+
if (s.depthFunc !== undefined)
|
|
684
|
+
c.depthFunc = s.depthFunc === 'less' ? 'lequal' : 'less';
|
|
685
|
+
if (s.depthWrite !== undefined)
|
|
686
|
+
c.depthWrite = !s.depthWrite;
|
|
687
|
+
if (s.blend !== undefined)
|
|
688
|
+
c.blend = s.blend ? null : { src: 'one', dst: 'one' };
|
|
689
|
+
if (s.alphaToCoverage !== undefined)
|
|
690
|
+
c.alphaToCoverage = !s.alphaToCoverage;
|
|
691
|
+
if (s.polygonOffset !== undefined)
|
|
692
|
+
c.polygonOffset = s.polygonOffset ? null : [0, 0];
|
|
693
|
+
}
|
|
694
|
+
if (s.depthTest !== undefined && s.depthTest !== c.depthTest) {
|
|
695
|
+
if (s.depthTest)
|
|
696
|
+
gl.enable(gl.DEPTH_TEST);
|
|
697
|
+
else
|
|
698
|
+
gl.disable(gl.DEPTH_TEST);
|
|
699
|
+
c.depthTest = s.depthTest;
|
|
700
|
+
}
|
|
701
|
+
if (s.depthFunc !== undefined && s.depthFunc !== c.depthFunc) {
|
|
702
|
+
c.depthFunc = s.depthFunc;
|
|
703
|
+
gl.depthFunc(this.depthFunc(s.depthFunc));
|
|
704
|
+
}
|
|
705
|
+
if (s.depthWrite !== undefined && s.depthWrite !== c.depthWrite) {
|
|
706
|
+
gl.depthMask(s.depthWrite);
|
|
707
|
+
c.depthWrite = s.depthWrite;
|
|
708
|
+
}
|
|
709
|
+
// `cull` wins over `cullBack`; true culls the back faces, false nothing. The face is switched only on a change.
|
|
710
|
+
const cull = s.cull ?? (s.cullBack === undefined ? undefined : s.cullBack ? 'back' : 'none');
|
|
711
|
+
if (cull !== undefined) {
|
|
712
|
+
const on = cull !== 'none';
|
|
713
|
+
if (force || on !== (c.cull !== 'none')) {
|
|
714
|
+
if (on)
|
|
715
|
+
gl.enable(gl.CULL_FACE);
|
|
716
|
+
else
|
|
717
|
+
gl.disable(gl.CULL_FACE);
|
|
718
|
+
}
|
|
719
|
+
if (on && (force || cull !== this.cullSide)) {
|
|
720
|
+
gl.cullFace(cull === 'front' ? gl.FRONT : gl.BACK);
|
|
721
|
+
this.cullSide = cull;
|
|
722
|
+
}
|
|
723
|
+
c.cull = cull;
|
|
724
|
+
}
|
|
725
|
+
if (s.blend !== undefined && !sameBlend(s.blend, c.blend)) {
|
|
726
|
+
if (s.blend) {
|
|
727
|
+
gl.enable(gl.BLEND);
|
|
728
|
+
const f = (b) => blendFactor(gl, b);
|
|
729
|
+
gl.blendFuncSeparate(f(s.blend.src), f(s.blend.dst), f(s.blend.srcAlpha ?? s.blend.src), f(s.blend.dstAlpha ?? s.blend.dst));
|
|
730
|
+
}
|
|
731
|
+
else
|
|
732
|
+
gl.disable(gl.BLEND);
|
|
733
|
+
c.blend = s.blend;
|
|
734
|
+
}
|
|
735
|
+
if (s.alphaToCoverage !== undefined && s.alphaToCoverage !== c.alphaToCoverage) {
|
|
736
|
+
if (s.alphaToCoverage)
|
|
737
|
+
gl.enable(gl.SAMPLE_ALPHA_TO_COVERAGE);
|
|
738
|
+
else
|
|
739
|
+
gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
|
|
740
|
+
c.alphaToCoverage = s.alphaToCoverage;
|
|
741
|
+
}
|
|
742
|
+
if (s.polygonOffset !== undefined && !samePair(s.polygonOffset, c.polygonOffset)) {
|
|
743
|
+
c.polygonOffset = s.polygonOffset;
|
|
744
|
+
this.applyPolygonOffset(s.polygonOffset);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Starts compiling and linking a program. `attribs` fixes attribute locations so one vertex array serves several
|
|
749
|
+
* programs. The status is checked on first use; programs made together compile in parallel in the driver.
|
|
750
|
+
*/
|
|
751
|
+
program(label, vs, fs, attribs = {}) {
|
|
752
|
+
const p = new Program(this.gl, label, vs, fs, attribs);
|
|
753
|
+
this.compilingPrograms.push(p);
|
|
754
|
+
this.programs.push(p);
|
|
755
|
+
return p;
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Number of programs the driver is still compiling (`KHR_parallel_shader_compile`; 0 without it). A loading screen
|
|
759
|
+
* can poll it between frames; nothing blocks.
|
|
760
|
+
*/
|
|
761
|
+
compiling() {
|
|
762
|
+
this.compilingPrograms = this.compilingPrograms.filter((p) => !p.isComplete());
|
|
763
|
+
return this.compilingPrograms.length;
|
|
764
|
+
}
|
|
765
|
+
use(p) {
|
|
766
|
+
p.use();
|
|
767
|
+
this.current = p;
|
|
768
|
+
if (this.hasFixedSamplers && !this.samplersSet.has(p)) {
|
|
769
|
+
// A sampler uniform keeps its unit for the life of the program, so once is enough.
|
|
770
|
+
this.samplersSet.add(p);
|
|
771
|
+
for (const name in this.fixedSamplers)
|
|
772
|
+
p.int(name, this.fixedSamplers[name]);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Gives a sampler uniform a fixed texture unit in every program used from now on (set once per program, at its
|
|
777
|
+
* first `use`; a program without the uniform ignores it). For a texture the renderer binds itself for every layer.
|
|
778
|
+
*/
|
|
779
|
+
fixSampler(name, unit) {
|
|
780
|
+
this.fixedSamplers[name] = unit;
|
|
781
|
+
this.hasFixedSamplers = true;
|
|
782
|
+
}
|
|
783
|
+
draw(m) {
|
|
784
|
+
if (this.biasInProgram)
|
|
785
|
+
this.syncDepthBias();
|
|
786
|
+
drawMesh(this.gl, m);
|
|
787
|
+
}
|
|
788
|
+
/** One triangle covering the whole viewport, without vertex data: the vertex shader builds it from `gl_VertexID`. */
|
|
789
|
+
drawFullscreen() {
|
|
790
|
+
if (this.biasInProgram)
|
|
791
|
+
this.syncDepthBias();
|
|
792
|
+
this.gl.bindVertexArray(this.emptyVao);
|
|
793
|
+
this.gl.drawArrays(this.gl.TRIANGLES, 0, 3);
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Overwrites an array buffer with `count` elements of `data` (floats, or bytes for a byte array), starting at the
|
|
797
|
+
* start of the buffer or `offsetBytes` into it.
|
|
798
|
+
*/
|
|
799
|
+
writeBuffer(buffer, data, count, offsetBytes = 0) {
|
|
800
|
+
// A length of 0 would mean "everything from the offset on" to WebGL.
|
|
801
|
+
if (count <= 0)
|
|
802
|
+
return;
|
|
803
|
+
const gl = this.gl;
|
|
804
|
+
if (this.retainUploads) {
|
|
805
|
+
// A write into a mesh's vertices is kept, so a restore after a context loss ends with the same bytes.
|
|
806
|
+
const owner = this.vertexOwner.get(buffer);
|
|
807
|
+
const recipe = owner ? this.meshRecipes.get(owner) : undefined;
|
|
808
|
+
if (recipe) {
|
|
809
|
+
// Taken out first, so the write goes to the end of the list: a restore replays the writes in their order,
|
|
810
|
+
// which matters where two of them overlap.
|
|
811
|
+
const patches = (recipe.patches ??= new Map());
|
|
812
|
+
patches.delete(offsetBytes);
|
|
813
|
+
patches.set(offsetBytes, new Uint8Array(data.buffer, data.byteOffset, count * data.BYTES_PER_ELEMENT).slice());
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
817
|
+
gl.bufferSubData(gl.ARRAY_BUFFER, offsetBytes, data, 0, count);
|
|
818
|
+
}
|
|
819
|
+
/** Unbinds the vertex array, so later buffer uploads cannot change a mesh by accident. */
|
|
820
|
+
unbindMesh() {
|
|
821
|
+
this.gl.bindVertexArray(null);
|
|
822
|
+
}
|
|
823
|
+
/** Uploads a mesh. `locations` are the attribute locations of the material it is drawn with. */
|
|
824
|
+
createMesh(data, locations, instances) {
|
|
825
|
+
if (!this.retainUploads)
|
|
826
|
+
return this.buildMesh(data, locations, instances);
|
|
827
|
+
const m = this.gl.isContextLost() ? this.emptyMesh(data, instances) : this.buildMesh(data, locations, instances);
|
|
828
|
+
this.meshRecipes.set(m, { data, base: null, locations, instances, patches: null });
|
|
829
|
+
const vb = m.buffers?.[0];
|
|
830
|
+
if (vb)
|
|
831
|
+
this.vertexOwner.set(vb, m);
|
|
832
|
+
return m;
|
|
833
|
+
}
|
|
834
|
+
/** A mesh object with nothing behind it yet: what `createMesh` hands out while the context is lost. */
|
|
835
|
+
emptyMesh(data, instances) {
|
|
836
|
+
const gl = this.gl;
|
|
837
|
+
const lines = data.primitive === 'lines';
|
|
838
|
+
const indexType = data.indices ? (data.indices instanceof Uint32Array ? gl.UNSIGNED_INT : gl.UNSIGNED_SHORT) : null;
|
|
839
|
+
const mode = lines ? { mode: gl.LINES } : {};
|
|
840
|
+
const none = null;
|
|
841
|
+
return {
|
|
842
|
+
vao: null,
|
|
843
|
+
count: data.count,
|
|
844
|
+
indexType,
|
|
845
|
+
triangles: lines ? 0 : data.count / 3,
|
|
846
|
+
instances: instances ? (instances.floats * 4) / instances.layout.stride : 0,
|
|
847
|
+
...mode,
|
|
848
|
+
buffers: [],
|
|
849
|
+
bytes: data.vertices.byteLength + (data.indices?.byteLength ?? 0),
|
|
850
|
+
source: { vertices: none, indices: null, layout: data.layout, indexType, count: data.count, ...mode },
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
buildMesh(data, locations, instances) {
|
|
854
|
+
const gl = this.gl;
|
|
855
|
+
const vao = gl.createVertexArray();
|
|
856
|
+
if (!vao)
|
|
857
|
+
throw new Error('Cannot create a VAO');
|
|
858
|
+
gl.bindVertexArray(vao);
|
|
859
|
+
const vb = createBuffer(gl, gl.ARRAY_BUFFER, data.vertices);
|
|
860
|
+
this.track(vb, 'buffers', data.vertices.byteLength);
|
|
861
|
+
bindAttribs(gl, vb, data.layout.stride, this.attribSpecs(data.layout, locations, 0));
|
|
862
|
+
if (instances)
|
|
863
|
+
bindAttribs(gl, instances.buffer, instances.layout.stride, this.attribSpecs(instances.layout, locations, 1));
|
|
864
|
+
const buffers = [vb];
|
|
865
|
+
let ib = null;
|
|
866
|
+
if (data.indices) {
|
|
867
|
+
ib = createBuffer(gl, gl.ELEMENT_ARRAY_BUFFER, data.indices);
|
|
868
|
+
this.track(ib, 'buffers', data.indices.byteLength);
|
|
869
|
+
buffers.push(ib);
|
|
870
|
+
}
|
|
871
|
+
gl.bindVertexArray(null);
|
|
872
|
+
const lines = data.primitive === 'lines';
|
|
873
|
+
const indexType = data.indices ? (data.indices instanceof Uint32Array ? gl.UNSIGNED_INT : gl.UNSIGNED_SHORT) : null;
|
|
874
|
+
const mode = lines ? { mode: gl.LINES } : {};
|
|
875
|
+
return {
|
|
876
|
+
vao,
|
|
877
|
+
count: data.count,
|
|
878
|
+
indexType,
|
|
879
|
+
triangles: lines ? 0 : data.count / 3,
|
|
880
|
+
instances: instances ? (instances.floats * 4) / instances.layout.stride : 0,
|
|
881
|
+
...mode,
|
|
882
|
+
buffers,
|
|
883
|
+
bytes: data.vertices.byteLength + (data.indices?.byteLength ?? 0),
|
|
884
|
+
source: { vertices: vb, indices: ib, layout: data.layout, indexType, count: data.count, ...mode },
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* A mesh that draws the vertex and index buffers of `base` (no new upload, `bytes` 0), with its own attribute
|
|
889
|
+
* locations and optionally its own instance buffer. Deleting it leaves `base`'s buffers alone; delete `base` last.
|
|
890
|
+
*/
|
|
891
|
+
shareMesh(base, locations, instances) {
|
|
892
|
+
const m = this.buildShared(base, locations, instances);
|
|
893
|
+
if (this.retainUploads)
|
|
894
|
+
this.meshRecipes.set(m, { data: null, base, locations, instances, patches: null });
|
|
895
|
+
return m;
|
|
896
|
+
}
|
|
897
|
+
buildShared(base, locations, instances) {
|
|
898
|
+
const src = base.source;
|
|
899
|
+
if (!src)
|
|
900
|
+
throw new Error('shareMesh: the base mesh has no source buffers');
|
|
901
|
+
const gl = this.gl;
|
|
902
|
+
const vao = gl.createVertexArray();
|
|
903
|
+
if (!vao && !(this.retainUploads && gl.isContextLost()))
|
|
904
|
+
throw new Error('Cannot create a VAO');
|
|
905
|
+
gl.bindVertexArray(vao);
|
|
906
|
+
bindAttribs(gl, src.vertices, src.layout.stride, this.attribSpecs(src.layout, locations, 0));
|
|
907
|
+
if (instances)
|
|
908
|
+
bindAttribs(gl, instances.buffer, instances.layout.stride, this.attribSpecs(instances.layout, locations, 1));
|
|
909
|
+
if (src.indices)
|
|
910
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, src.indices);
|
|
911
|
+
gl.bindVertexArray(null);
|
|
912
|
+
return {
|
|
913
|
+
vao: vao,
|
|
914
|
+
count: src.count,
|
|
915
|
+
indexType: src.indexType,
|
|
916
|
+
triangles: src.mode !== undefined ? 0 : src.count / 3,
|
|
917
|
+
instances: instances ? (instances.floats * 4) / instances.layout.stride : 0,
|
|
918
|
+
...(src.mode !== undefined ? { mode: src.mode } : {}),
|
|
919
|
+
buffers: [],
|
|
920
|
+
bytes: 0,
|
|
921
|
+
source: src,
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
/** Uploads a per-instance stream that is rewritten often (culling). */
|
|
925
|
+
createInstances(data) {
|
|
926
|
+
const gl = this.gl;
|
|
927
|
+
if (this.retainUploads && gl.isContextLost()) {
|
|
928
|
+
const waiting = { buffer: null, layout: data.layout, floats: data.data.length };
|
|
929
|
+
this.instanceRecipes.set(waiting, data);
|
|
930
|
+
return waiting;
|
|
931
|
+
}
|
|
932
|
+
const buffer = createBuffer(gl, gl.ARRAY_BUFFER, data.data, gl.DYNAMIC_DRAW);
|
|
933
|
+
this.track(buffer, 'buffers', data.data.byteLength);
|
|
934
|
+
const b = { buffer, layout: data.layout, floats: data.data.length };
|
|
935
|
+
if (this.retainUploads)
|
|
936
|
+
this.instanceRecipes.set(b, data);
|
|
937
|
+
return b;
|
|
938
|
+
}
|
|
939
|
+
deleteMesh(m) {
|
|
940
|
+
this.gl.deleteVertexArray(m.vao);
|
|
941
|
+
for (const b of m.buffers ?? []) {
|
|
942
|
+
this.untrack(b);
|
|
943
|
+
this.vertexOwner.delete(b);
|
|
944
|
+
this.gl.deleteBuffer(b);
|
|
945
|
+
}
|
|
946
|
+
this.meshRecipes.delete(m);
|
|
947
|
+
}
|
|
948
|
+
deleteInstances(b) {
|
|
949
|
+
this.untrack(b.buffer);
|
|
950
|
+
this.instanceRecipes.delete(b);
|
|
951
|
+
this.gl.deleteBuffer(b.buffer);
|
|
952
|
+
}
|
|
953
|
+
/** Uploads a texture. */
|
|
954
|
+
createTexture(data) {
|
|
955
|
+
const gl = this.gl;
|
|
956
|
+
const make = () => {
|
|
957
|
+
const f = textureFormat(gl, data.format);
|
|
958
|
+
const made = createTexture(gl, { ...f, width: data.width, height: data.height, data: data.data, ...sampling(gl, data) });
|
|
959
|
+
if (data.filter === 'mipmap')
|
|
960
|
+
this.finishMipmaps(made, data);
|
|
961
|
+
return made;
|
|
962
|
+
};
|
|
963
|
+
const t = this.newTexture(make);
|
|
964
|
+
this.track(t, 'textures', textureBytes(data.width, data.height, texelBytes(data.format), data.filter === 'mipmap'));
|
|
965
|
+
return t;
|
|
966
|
+
}
|
|
967
|
+
deleteTexture(t) {
|
|
968
|
+
this.untrack(t);
|
|
969
|
+
this.textureRecipes.delete(t);
|
|
970
|
+
const bake = this.bakeOf.get(t);
|
|
971
|
+
if (bake) {
|
|
972
|
+
this.bakeOf.delete(t);
|
|
973
|
+
bake.alive--;
|
|
974
|
+
}
|
|
975
|
+
const live = this.liveTexture.get(t);
|
|
976
|
+
if (live !== undefined) {
|
|
977
|
+
this.liveTexture.delete(t);
|
|
978
|
+
this.gl.deleteTexture(live);
|
|
979
|
+
}
|
|
980
|
+
else
|
|
981
|
+
this.gl.deleteTexture(t);
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Runs `fs` once over a full-screen triangle of `width × height` into new RGBA8 targets and returns them.
|
|
985
|
+
* Inputs are uploaded, bound to the units 0, 1, … and deleted afterwards.
|
|
986
|
+
*/
|
|
987
|
+
bake(label, fs, width, height, inputs, uniforms, targets) {
|
|
988
|
+
const run = () => this.runBake(label, fs, width, height, inputs, uniforms, targets);
|
|
989
|
+
const lost = this.retainUploads && this.gl.isContextLost();
|
|
990
|
+
// While the context is lost the caller gets handles with nothing behind them; the bake runs on restore.
|
|
991
|
+
const outs = lost ? targets.map(() => ({})) : run();
|
|
992
|
+
outs.forEach((t, i) => this.track(t, 'textures', textureBytes(width, height, 4, targets[i].filter === 'mipmap')));
|
|
993
|
+
if (this.retainUploads) {
|
|
994
|
+
const recipe = { keys: outs.slice(), alive: outs.length, run };
|
|
995
|
+
for (const t of outs) {
|
|
996
|
+
this.bakeOf.set(t, recipe);
|
|
997
|
+
if (lost)
|
|
998
|
+
this.liveTexture.set(t, null);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
return outs;
|
|
1002
|
+
}
|
|
1003
|
+
runBake(label, fs, width, height, inputs, uniforms, targets) {
|
|
1004
|
+
const gl = this.gl;
|
|
1005
|
+
const inputTextures = inputs.map((i) => this.createTexture(i.texture));
|
|
1006
|
+
const outs = targets.map((t) => createTexture(gl, { ...textureFormat(gl, 'rgba8'), width, height, ...sampling(gl, t) }));
|
|
1007
|
+
const fbo = gl.createFramebuffer();
|
|
1008
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
1009
|
+
outs.forEach((t, i) => gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, t, 0));
|
|
1010
|
+
gl.drawBuffers(outs.map((_, i) => gl.COLOR_ATTACHMENT0 + i));
|
|
1011
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
|
|
1012
|
+
throw new Error(`Bake target of ${label} is incomplete`);
|
|
1013
|
+
const prog = new Program(gl, label, FULLSCREEN_VS, fs);
|
|
1014
|
+
prog.use();
|
|
1015
|
+
// The bake program is used behind the device's back and deleted below.
|
|
1016
|
+
this.current = null;
|
|
1017
|
+
gl.viewport(0, 0, width, height);
|
|
1018
|
+
this.setState({ depthTest: false, cullBack: false, blend: null, alphaToCoverage: false, polygonOffset: null });
|
|
1019
|
+
inputs.forEach((input, unit) => {
|
|
1020
|
+
this.texture(unit, inputTextures[unit]);
|
|
1021
|
+
prog.int(input.sampler, unit);
|
|
1022
|
+
});
|
|
1023
|
+
for (const [name, v] of Object.entries(uniforms))
|
|
1024
|
+
prog.set(name, v);
|
|
1025
|
+
this.drawFullscreen();
|
|
1026
|
+
gl.bindVertexArray(null);
|
|
1027
|
+
// Back to the canvas, as ever. With an offscreen main target: back to whatever `target` bound, so a bake in the
|
|
1028
|
+
// middle of a pass does not send the rest of the pass to the canvas.
|
|
1029
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.offscreen() ? this.boundFbo : null);
|
|
1030
|
+
gl.deleteFramebuffer(fbo);
|
|
1031
|
+
for (const t of inputTextures)
|
|
1032
|
+
this.deleteTexture(t);
|
|
1033
|
+
gl.deleteProgram(prog.handle);
|
|
1034
|
+
outs.forEach((t, i) => {
|
|
1035
|
+
if (targets[i].filter === 'mipmap')
|
|
1036
|
+
this.finishMipmaps(t, targets[i]);
|
|
1037
|
+
});
|
|
1038
|
+
return outs;
|
|
1039
|
+
}
|
|
1040
|
+
finishMipmaps(t, s) {
|
|
1041
|
+
const gl = this.gl;
|
|
1042
|
+
gl.bindTexture(gl.TEXTURE_2D, t);
|
|
1043
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
1044
|
+
const aniso = s.anisotropy ? gl.getExtension('EXT_texture_filter_anisotropic') : null;
|
|
1045
|
+
if (aniso && s.anisotropy) {
|
|
1046
|
+
const max = gl.getParameter(aniso.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
|
|
1047
|
+
gl.texParameterf(gl.TEXTURE_2D, aniso.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(s.anisotropy, max));
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
attribSpecs(layout, locations, divisor) {
|
|
1051
|
+
const specs = [];
|
|
1052
|
+
for (const a of layout.attribs) {
|
|
1053
|
+
const loc = locations[a.name];
|
|
1054
|
+
if (loc === undefined)
|
|
1055
|
+
continue;
|
|
1056
|
+
specs.push({ loc, size: a.size, type: scalarType(this.gl, a.type), normalized: a.normalized ?? false, integer: a.integer ?? false, offset: a.offset, divisor });
|
|
1057
|
+
}
|
|
1058
|
+
return specs;
|
|
1059
|
+
}
|
|
1060
|
+
/** Hands the queued commands to the graphics card without waiting for them. */
|
|
1061
|
+
flush() {
|
|
1062
|
+
this.gl.flush();
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Samples per pixel of the main target: the offscreen target's in the reversed and logarithmic depth modes, the
|
|
1066
|
+
* canvas's otherwise (0 without antialiasing). Queries the context; meant for statistics, not for every frame.
|
|
1067
|
+
*/
|
|
1068
|
+
mainSamples() {
|
|
1069
|
+
const gl = this.gl;
|
|
1070
|
+
if (this.offscreen())
|
|
1071
|
+
return Math.min(this.sceneSamples, gl.getParameter(gl.MAX_SAMPLES));
|
|
1072
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
1073
|
+
const samples = gl.getParameter(gl.SAMPLES);
|
|
1074
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.boundFbo);
|
|
1075
|
+
return samples;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Waits until the graphics card has finished all work (by reading one pixel). A read into a client array has to
|
|
1079
|
+
* return the pixel, so the call blocks until every command before it that the bound target depends on is done.
|
|
1080
|
+
*/
|
|
1081
|
+
finish() {
|
|
1082
|
+
const px = new Uint8Array(4);
|
|
1083
|
+
this.gl.readPixels(0, 0, 1, 1, this.gl.RGBA, this.gl.UNSIGNED_BYTE, px);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
/** Bytes of a 2D texture; a full mipmap chain adds a third. */
|
|
1087
|
+
function textureBytes(width, height, texel, mipmaps) {
|
|
1088
|
+
const base = width * height * texel;
|
|
1089
|
+
return mipmaps ? Math.round((base * 4) / 3) : base;
|
|
1090
|
+
}
|
|
1091
|
+
/** Bytes of the textures of a colour target. */
|
|
1092
|
+
function colorTargetBytes(t) {
|
|
1093
|
+
return t.width * t.height * t.textures.length * (t.format === 'rgba16f' ? 8 : 4);
|
|
1094
|
+
}
|
|
1095
|
+
function blendFactor(gl, b) {
|
|
1096
|
+
switch (b) {
|
|
1097
|
+
case 'zero':
|
|
1098
|
+
return gl.ZERO;
|
|
1099
|
+
case 'one':
|
|
1100
|
+
return gl.ONE;
|
|
1101
|
+
case 'src-alpha':
|
|
1102
|
+
return gl.SRC_ALPHA;
|
|
1103
|
+
case 'one-minus-src-alpha':
|
|
1104
|
+
return gl.ONE_MINUS_SRC_ALPHA;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
function sameBlend(a, b) {
|
|
1108
|
+
if (a === b)
|
|
1109
|
+
return true;
|
|
1110
|
+
if (!a || !b)
|
|
1111
|
+
return false;
|
|
1112
|
+
return a.src === b.src && a.dst === b.dst && (a.srcAlpha ?? a.src) === (b.srcAlpha ?? b.src) && (a.dstAlpha ?? a.dst) === (b.dstAlpha ?? b.dst);
|
|
1113
|
+
}
|
|
1114
|
+
function samePair(a, b) {
|
|
1115
|
+
if (a === b)
|
|
1116
|
+
return true;
|
|
1117
|
+
if (!a || !b)
|
|
1118
|
+
return false;
|
|
1119
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
1120
|
+
}
|
|
1121
|
+
function scalarType(gl, t) {
|
|
1122
|
+
switch (t) {
|
|
1123
|
+
case 'f32':
|
|
1124
|
+
return gl.FLOAT;
|
|
1125
|
+
case 'i8':
|
|
1126
|
+
return gl.BYTE;
|
|
1127
|
+
case 'u8':
|
|
1128
|
+
return gl.UNSIGNED_BYTE;
|
|
1129
|
+
case 'i16':
|
|
1130
|
+
return gl.SHORT;
|
|
1131
|
+
case 'u16':
|
|
1132
|
+
return gl.UNSIGNED_SHORT;
|
|
1133
|
+
case 'i32':
|
|
1134
|
+
return gl.INT;
|
|
1135
|
+
case 'u32':
|
|
1136
|
+
return gl.UNSIGNED_INT;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
function textureFormat(gl, f) {
|
|
1140
|
+
switch (f) {
|
|
1141
|
+
case 'r8':
|
|
1142
|
+
return { internalFormat: gl.R8, format: gl.RED, type: gl.UNSIGNED_BYTE };
|
|
1143
|
+
case 'r8ui':
|
|
1144
|
+
return { internalFormat: gl.R8UI, format: gl.RED_INTEGER, type: gl.UNSIGNED_BYTE };
|
|
1145
|
+
case 'rg8':
|
|
1146
|
+
return { internalFormat: gl.RG8, format: gl.RG, type: gl.UNSIGNED_BYTE };
|
|
1147
|
+
case 'rgba8':
|
|
1148
|
+
return { internalFormat: gl.RGBA8, format: gl.RGBA, type: gl.UNSIGNED_BYTE };
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
function sampling(gl, s) {
|
|
1152
|
+
if (s.filter === 'mipmap')
|
|
1153
|
+
return { minFilter: gl.LINEAR_MIPMAP_LINEAR, magFilter: gl.LINEAR };
|
|
1154
|
+
if (s.filter === 'linear')
|
|
1155
|
+
return { minFilter: gl.LINEAR, magFilter: gl.LINEAR };
|
|
1156
|
+
return { minFilter: gl.NEAREST, magFilter: gl.NEAREST };
|
|
1157
|
+
}
|