Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(render): the scene pass writes each surface's diffuse colour beside its normal, so Voxel GI and SSGI reflect bounce light by what it lands on instead of a grey guessed from the lit picture; render gates hold the baked and voxel-lit GI room

yhh1f445e469aed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 28d241687..5d95d8aaf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@ published separately; it ships inside the editor.
### Fixed
- **Baked lighting shows on built-in meshes.** A `builtin:` cube, sphere or plane baked with `bake-scene` or the editor drew as if nothing had been baked: the bake unwrapped a lightmap UV set for its own use, but the mesh the game loaded had none, so the atlas was never read. The built-in meshes now carry that UV set, the bake lays out its charts by it, and existing bakes stay valid.
+- **Bounce light takes the colour of what it lands on.** Voxel GI and Screen Space GI guessed a surface's colour from the lit picture, so a wall the sun did not reach got its bounce as a dim grey: a red wall in a sunlit room received 15% of the red light a path tracer gives it. The scene pass now writes each surface's diffuse colour beside its normal, and both effects reflect the bounce by it. In the same room the red wall gets 50–75% and keeps its hue, and the ceiling went from 14% to 57–90%. A custom shader that writes the surface record keeps working and has its colour guessed as before until it also writes `fragAlbedo` (location 2).
- **Baked lighting reflects light 8 times by default, not 2.** Two bounces left a room of light walls at about half the light a path tracer gives it, and a face only bounced light reaches at a tenth. Eight is within about 10% of Cycles in a measured room and cost a 252k-lumel bake 0.7 s more. The inspector now goes to 16. A scene that states its own bounces keeps them.
## [0.98.0] - 2026-11-05
diff --git a/fixtures/scenes/gi-room-baked.esscene b/fixtures/scenes/gi-room-baked.esscene
new file mode 100644
index 000000000..50305e8ad
--- /dev/null
+++ b/fixtures/scenes/gi-room-baked.esscene
@@ -0,0 +1,611 @@
+{
+ "version": 4,
+ "name": "GiRoomBaked",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 160,
+ "z": -235
+ },
+ "rotation": {
+ "w": 0,
+ "x": 0,
+ "y": 0.9986811118055354,
+ "z": 0.05134235018831678
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 0,
+ "fov": 62,
+ "nearPlane": 5,
+ "farPlane": 5000,
+ "isActive": true,
+ "priority": 0,
+ "clearFlags": 3
+ }
+ }
+ ]
+ },
+ {
+ "id": 10,
+ "name": "floor",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": -5,
+ "z": 0
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 0.1,
+ "z": 5.2
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.1865234375,
+ "y": 0.1865234375,
+ "z": 0,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 11,
+ "name": "left",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 205,
+ "y": 150,
+ "z": 0
+ },
+ "scale": {
+ "x": 0.1,
+ "y": 3,
+ "z": 5
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.8,
+ "g": 0.18,
+ "b": 0.15,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.1552734375,
+ "y": 0.1552734375,
+ "z": 0.1865234375,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 12,
+ "name": "right",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -205,
+ "y": 150,
+ "z": 0
+ },
+ "scale": {
+ "x": 0.1,
+ "y": 3,
+ "z": 5
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.18,
+ "g": 0.7,
+ "b": 0.22,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.1552734375,
+ "y": 0.1552734375,
+ "z": 0.341796875,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 13,
+ "name": "back",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 150,
+ "z": 255
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 3,
+ "z": 0.1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.142578125,
+ "y": 0.142578125,
+ "z": 0.4970703125,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 14,
+ "name": "front",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 150,
+ "z": -255
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 3,
+ "z": 0.1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.142578125,
+ "y": 0.142578125,
+ "z": 0.6396484375,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 15,
+ "name": "ceil_n",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 305,
+ "z": -130
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 0.1,
+ "z": 2.6
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.1337890625,
+ "y": 0.1337890625,
+ "z": 0.7822265625,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 16,
+ "name": "ceil_s",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 305,
+ "z": 190
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 0.1,
+ "z": 1.4
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.099609375,
+ "y": 0.099609375,
+ "z": 0.7822265625,
+ "w": 0.1337890625
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 17,
+ "name": "ceil_l",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 135,
+ "y": 305,
+ "z": 60
+ },
+ "scale": {
+ "x": 1.5,
+ "y": 0.1,
+ "z": 1.2
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.056640625,
+ "y": 0.056640625,
+ "z": 0.916015625,
+ "w": 0.068359375
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 18,
+ "name": "ceil_r",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -135,
+ "y": 305,
+ "z": 60
+ },
+ "scale": {
+ "x": 1.5,
+ "y": 0.1,
+ "z": 1.2
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.056640625,
+ "y": 0.056640625,
+ "z": 0.916015625,
+ "w": 0.125
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 19,
+ "name": "block",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -90,
+ "y": 50,
+ "z": 80
+ },
+ "scale": {
+ "x": 1,
+ "y": 1,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "MeshLightmap",
+ "data": {
+ "lightmap": "@uuid:f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "scaleOffset": {
+ "x": 0.068359375,
+ "y": 0.068359375,
+ "z": 0.916015625,
+ "w": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 5,
+ "name": "Sun",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "rotation": {
+ "w": 0.5692033680809173,
+ "x": -0.814457456297922,
+ "y": -0.1125458912891015,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 1,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "enabled": true,
+ "meshShadows": true,
+ "shadowExtent": 0,
+ "shadowStrength": 1,
+ "sourceAngle": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 6,
+ "name": "Lighting",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "BakedLighting",
+ "data": {
+ "atlasSize": 1024,
+ "texelsPerUnit": 0.2,
+ "bounces": 8,
+ "samples": 256,
+ "probeSamples": 128,
+ "bakedFrom": "afd4bbd19b80cded"
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/fixtures/scenes/gi-room-baked.textures.json b/fixtures/scenes/gi-room-baked.textures.json
new file mode 100644
index 000000000..f0b6265df
--- /dev/null
+++ b/fixtures/scenes/gi-room-baked.textures.json
@@ -0,0 +1,3 @@
+{
+ "f8da09ee-644a-45a3-963e-240b1921cdf0": "/scenes/gi-room-baked_lightmap.png"
+}
\ No newline at end of file
diff --git a/fixtures/scenes/gi-room-baked_lightmap.png b/fixtures/scenes/gi-room-baked_lightmap.png
new file mode 100644
index 000000000..437ab274a
Binary files /dev/null and b/fixtures/scenes/gi-room-baked_lightmap.png differ
diff --git a/fixtures/scenes/gi-room-baked_lightmap.png.meta b/fixtures/scenes/gi-room-baked_lightmap.png.meta
new file mode 100644
index 000000000..f3c257610
--- /dev/null
+++ b/fixtures/scenes/gi-room-baked_lightmap.png.meta
@@ -0,0 +1,28 @@
+{
+ "uuid": "f8da09ee-644a-45a3-963e-240b1921cdf0",
+ "version": "2.0",
+ "type": "texture",
+ "importer": {
+ "maxSize": 2048,
+ "compress": "off",
+ "compressFormat": "uastc",
+ "mipmaps": true,
+ "filterMode": "linear",
+ "wrapMode": "clamp",
+ "sRGB": false,
+ "alphaIsTransparency": true,
+ "mipCoverage": 0,
+ "sliceBorder": {
+ "left": 0,
+ "right": 0,
+ "top": 0,
+ "bottom": 0
+ },
+ "sheet": {
+ "cellWidth": 0,
+ "cellHeight": 0,
+ "margin": 0,
+ "spacing": 0
+ }
+ }
+}
diff --git a/fixtures/scenes/gi-room-voxel.esscene b/fixtures/scenes/gi-room-voxel.esscene
new file mode 100644
index 000000000..bb9875446
--- /dev/null
+++ b/fixtures/scenes/gi-room-voxel.esscene
@@ -0,0 +1,486 @@
+{
+ "version": 4,
+ "name": "GiRoomVoxel",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0.0,
+ "y": 160.0,
+ "z": -235.0
+ },
+ "rotation": {
+ "w": -0.0,
+ "x": 0.0,
+ "y": 0.9986811118055354,
+ "z": 0.05134235018831678
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 0,
+ "fov": 62.0,
+ "nearPlane": 5.0,
+ "farPlane": 5000.0,
+ "isActive": true,
+ "priority": 0,
+ "clearFlags": 3
+ }
+ },
+ {
+ "type": "PostProcessVolume",
+ "data": {
+ "isGlobal": true,
+ "weight": 1,
+ "priority": 0,
+ "effects": [
+ {
+ "type": "voxelGI",
+ "enabled": true,
+ "uniforms": {}
+ }
+ ]
+ }
+ }
+ ]
+ },
+ {
+ "id": 10,
+ "name": "floor",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": -5.0,
+ "z": 0
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 0.1,
+ "z": 5.2
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 11,
+ "name": "left",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 205.0,
+ "y": 150.0,
+ "z": 0
+ },
+ "scale": {
+ "x": 0.1,
+ "y": 3.0,
+ "z": 5.0
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.8,
+ "g": 0.18,
+ "b": 0.15,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 12,
+ "name": "right",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -205.0,
+ "y": 150.0,
+ "z": 0
+ },
+ "scale": {
+ "x": 0.1,
+ "y": 3.0,
+ "z": 5.0
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.18,
+ "g": 0.7,
+ "b": 0.22,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 13,
+ "name": "back",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 150.0,
+ "z": 255.0
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 3.0,
+ "z": 0.1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 14,
+ "name": "front",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 150.0,
+ "z": -255.0
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 3.0,
+ "z": 0.1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 15,
+ "name": "ceil_n",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 305.0,
+ "z": -130.0
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 0.1,
+ "z": 2.6
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 16,
+ "name": "ceil_s",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 305.0,
+ "z": 190.0
+ },
+ "scale": {
+ "x": 4.2,
+ "y": 0.1,
+ "z": 1.4
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 17,
+ "name": "ceil_l",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 135.0,
+ "y": 305.0,
+ "z": 60.0
+ },
+ "scale": {
+ "x": 1.5,
+ "y": 0.1,
+ "z": 1.2
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 18,
+ "name": "ceil_r",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -135.0,
+ "y": 305.0,
+ "z": 60.0
+ },
+ "scale": {
+ "x": 1.5,
+ "y": 0.1,
+ "z": 1.2
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 19,
+ "name": "block",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -90,
+ "y": 50,
+ "z": 80
+ },
+ "scale": {
+ "x": 1.0,
+ "y": 1.0,
+ "z": 1.0
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "builtin:cube",
+ "lit": true,
+ "opaque": true,
+ "cullBackfaces": true,
+ "color": {
+ "r": 0.85,
+ "g": 0.85,
+ "b": 0.85,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 5,
+ "name": "Sun",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "rotation": {
+ "w": 0.5692033680809173,
+ "x": -0.814457456297922,
+ "y": -0.1125458912891015,
+ "z": -0.0
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 1,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1.0,
+ "enabled": true,
+ "meshShadows": true,
+ "shadowExtent": 0.0,
+ "shadowStrength": 1.0,
+ "sourceAngle": 0.0
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/fixtures/scenes/gi-room-voxel.textures.json b/fixtures/scenes/gi-room-voxel.textures.json
new file mode 100644
index 000000000..9e26dfeeb
--- /dev/null
+++ b/fixtures/scenes/gi-room-voxel.textures.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/sdk/src/character/characterShaders.ts b/sdk/src/character/characterShaders.ts
index 394887e27..c6248e280 100644
--- a/sdk/src/character/characterShaders.ts
+++ b/sdk/src/character/characterShaders.ts
@@ -79,6 +79,8 @@ uniform sampler2D u_textures[1];
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -148,6 +150,7 @@ void main() {
fragColor = vec4(lit, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), u_roughness, 0.75);
+ fragAlbedo = vec4(base.rgb, 1.0);
#endif
}
#pragma end
@@ -157,6 +160,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -241,6 +245,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(lit, alpha);
frag.surface = vec4f(packNormal(normalize(N)), mc.u_roughness, 0.75);
+ frag.albedo = vec4f(base.rgb, 1.0);
return frag;
#else
return vec4f(lit, alpha);
@@ -301,6 +306,8 @@ uniform sampler2D u_textures[1];
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -371,6 +378,7 @@ void main() {
fragColor = vec4(diffuse + spec, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), rough, 0.75);
+ fragAlbedo = vec4(base.rgb, 1.0);
#endif
}
#pragma end
@@ -380,6 +388,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -453,6 +462,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(diffuse + spec, alpha);
frag.surface = vec4f(packNormal(normalize(N)), rough, 0.75);
+ frag.albedo = vec4f(base.rgb, 1.0);
return frag;
#else
return vec4f(diffuse + spec, alpha);
@@ -505,6 +515,8 @@ uniform sampler2D u_textures[1];
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -573,6 +585,7 @@ void main() {
fragColor = vec4((diffuse + spec) * lidShade, 1.0);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(Nc)), u_corneaRoughness, 0.75);
+ fragAlbedo = vec4(albedo, 1.0);
#endif
}
#pragma end
@@ -582,6 +595,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -657,6 +671,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f((diffuse + spec) * lidShade, 1.0);
frag.surface = vec4f(packNormal(normalize(Nc)), mc.u_corneaRoughness, 0.75);
+ frag.albedo = vec4f(albedo, 1.0);
return frag;
#else
return vec4f((diffuse + spec) * lidShade, 1.0);
diff --git a/sdk/src/foliage/foliageShader.ts b/sdk/src/foliage/foliageShader.ts
index d25118de2..bbbf7a7a2 100644
--- a/sdk/src/foliage/foliageShader.ts
+++ b/sdk/src/foliage/foliageShader.ts
@@ -98,6 +98,8 @@ in highp vec3 v_worldXYZ;
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -143,6 +145,7 @@ void main() {
fragColor = vec4(lit, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(N), u_roughness, 0.75);
+ fragAlbedo = vec4(albedo, 1.0);
#endif
}
#pragma end
@@ -152,6 +155,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -205,6 +209,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(lit, alpha);
frag.surface = vec4f(packNormal(N), mc.u_roughness, 0.75);
+ frag.albedo = vec4f(albedo, 1.0);
return frag;
#else
return vec4f(lit, alpha);
diff --git a/sdk/src/postprocess/sources/lighting.ts b/sdk/src/postprocess/sources/lighting.ts
index 294e08c70..8b438ca16 100644
--- a/sdk/src/postprocess/sources/lighting.ts
+++ b/sdk/src/postprocess/sources/lighting.ts
@@ -696,6 +696,8 @@ ${WGSL}
const source = `#pragma shader "PP SSGI Apply"
#pragma version 300 es
#pragma domain PostProcess
+// The scene's albedo, beside its surface record (alpha 1 where a surface wrote one); black is "none written".
+#pragma param u_sceneAlbedo texture default(black) texel(data)
#pragma param u_intensity float default(1) range(0,4)
#pragma gbuffer
@@ -741,9 +743,12 @@ void main() {
}
}
vec3 gathered = wsum > 1e-4 ? sum / wsum : vec3(0.0);
- // The bounced light wears the receiver's own colour (its chroma, kept below white so a bright surface does not run away).
+ // The bounced light is reflected by the receiver's own diffuse colour, which it wrote beside its normal; a surface
+ // that wrote none (a shader older than the attachment) has it guessed from the picture, kept below white.
+ vec4 written = texelFetch(u_sceneAlbedo, min(c, textureSize(u_sceneAlbedo, 0) - ivec2(1)), 0);
float lum = max(dot(src.rgb, vec3(0.2126, 0.7152, 0.0722)), 0.02);
- vec3 albedo = mix(vec3(0.55), clamp(src.rgb / lum, vec3(0.0), vec3(1.6)) * 0.55, 0.7);
+ vec3 albedo = written.a > 0.5 ? written.rgb
+ : mix(vec3(0.55), clamp(src.rgb / lum, vec3(0.0), vec3(1.6)) * 0.55, 0.7);
fragColor = vec4(src.rgb + albedo * gathered * u_intensity, src.a);
}
#pragma end
@@ -781,8 +786,10 @@ ${GBUFFER_WGSL}
}
var gathered = vec3f(0.0);
if (wsum > 1e-4) { gathered = sum / wsum; }
+ let written = textureLoad(u_sceneAlbedo, min(c, vec2i(textureDimensions(u_sceneAlbedo, 0)) - vec2i(1)), 0);
let lum = max(dot(src.rgb, vec3f(0.2126, 0.7152, 0.0722)), 0.02);
- let albedo = mix(vec3f(0.55), clamp(src.rgb / lum, vec3f(0.0), vec3f(1.6)) * 0.55, 0.7);
+ let guessed = mix(vec3f(0.55), clamp(src.rgb / lum, vec3f(0.0), vec3f(1.6)) * 0.55, 0.7);
+ let albedo = select(guessed, written.rgb, written.a > 0.5);
return vec4f(src.rgb + albedo * gathered * mc.u_intensity, src.a);
}
#pragma end
diff --git a/sdk/src/postprocess/voxelGIFallback.ts b/sdk/src/postprocess/voxelGIFallback.ts
index 351c85dd8..f33174f2f 100644
--- a/sdk/src/postprocess/voxelGIFallback.ts
+++ b/sdk/src/postprocess/voxelGIFallback.ts
@@ -265,6 +265,8 @@ void main() {
const source = `#pragma shader "PP VoxelGI Apply"
#pragma version 300 es
#pragma domain PostProcess
+// The scene's albedo, beside its surface record (alpha 1 where a surface wrote one); black is "none written".
+#pragma param u_sceneAlbedo texture default(black) texel(data)
#pragma param u_intensity float default(1) range(0,4)
#pragma param u_debug float default(0) range(0,1)
@@ -309,9 +311,12 @@ void main() {
}
if (total > 1e-4) light = acc / total;
}
- // What the gathered light is lit onto wears the surface's own colour (its chroma, kept below white).
+ // What the gathered light is lit onto reflects it by its own diffuse colour, which the surface wrote beside its
+ // normal. A surface that wrote none (a shader older than the attachment) has its colour guessed from the picture.
+ vec4 written = texelFetch(u_sceneAlbedo, min(t, textureSize(u_sceneAlbedo, 0) - ivec2(1)), 0);
float lum = max(dot(src.rgb, vec3(0.2126, 0.7152, 0.0722)), 0.02);
- vec3 albedo = mix(vec3(0.55), clamp(src.rgb / lum, vec3(0.0), vec3(1.6)) * 0.55, 0.7);
+ vec3 albedo = written.a > 0.5 ? written.rgb
+ : mix(vec3(0.55), clamp(src.rgb / lum, vec3(0.0), vec3(1.6)) * 0.55, 0.7);
vec3 result = src.rgb + albedo * light * u_intensity;
if (u_debug > 0.5) result = light * u_intensity;
fragColor = vec4(result, src.a);
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index be8f4d6a1..0bbeb8662 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -516,6 +516,8 @@ uniform sampler2D u_textures[1];
// attachment asks for this variant.
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
// Octahedral normal: a unit vector in two channels, which is what an 8-bit target can hold.
vec2 packNormal(vec3 n) {
@@ -646,6 +648,7 @@ void main() {
// Alpha says something was drawn here (0 is "nothing wrote"), and carries the metalness above 0.75: a pixel that
// multisampling averaged with an unwritten one falls below it, and an effect can tell such an edge from a surface.
fragSurface = vec4(packNormal(normalize(N)), roughness, 0.75 + 0.25 * clamp(u_metallic * mr.b, 0.0, 1.0));
+ fragAlbedo = vec4(base.rgb * (1.0 - clamp(u_metallic * mr.b, 0.0, 1.0)), 1.0);
#endif
}
#pragma end
@@ -664,6 +667,7 @@ fn specularAA(dndx : vec3f, dndy : vec3f, roughness : f32) -> f32 {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -788,6 +792,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(lit + seen + emit, alpha);
frag.surface = vec4f(packNormal(normalize(N)), roughness, 0.75 + 0.25 * clamp(mc.u_metallic * mr.b, 0.0, 1.0));
+ frag.albedo = vec4f(base.rgb * (1.0 - clamp(mc.u_metallic * mr.b, 0.0, 1.0)), 1.0);
return frag;
#else
return vec4f(lit + seen + emit, alpha);
@@ -838,6 +843,8 @@ uniform sampler2D u_textures[1];
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
@@ -905,6 +912,7 @@ void main() {
fragColor = vec4(colour + u_emissive.rgb * texture(u_emissiveMap, v_texCoord).rgb, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), roughness, 0.75);
+ fragAlbedo = vec4(base.rgb, 1.0);
#endif
}
#pragma end
@@ -914,6 +922,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -981,6 +990,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(colour + emit, alpha);
frag.surface = vec4f(packNormal(normalize(N)), roughness, 0.75);
+ frag.albedo = vec4f(base.rgb, 1.0);
return frag;
#else
return vec4f(colour + emit, alpha);
@@ -1026,6 +1036,8 @@ uniform sampler2D u_textures[1];
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
@@ -1062,6 +1074,7 @@ void main() {
fragColor = vec4(colour, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), 1.0, 0.75);
+ fragAlbedo = vec4(base.rgb, 1.0);
#endif
}
#pragma end
@@ -1071,6 +1084,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -1115,6 +1129,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(colour, alpha);
frag.surface = vec4f(packNormal(normalize(N)), 1.0, 0.75);
+ frag.albedo = vec4f(base.rgb, 1.0);
return frag;
#else
return vec4f(colour, alpha);
diff --git a/sdk/src/terrain/planetSea.ts b/sdk/src/terrain/planetSea.ts
index 5a6107fe4..e1d80c48e 100644
--- a/sdk/src/terrain/planetSea.ts
+++ b/sdk/src/terrain/planetSea.ts
@@ -69,6 +69,8 @@ in highp vec3 v_worldXYZ;
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
#else
out vec4 fragColor;
#endif
@@ -88,6 +90,7 @@ void main() {
fragColor = vec4(lit, u_seaOpacity);
#ifdef ES_GBUFFER
fragSurface = vec4(0.5, 0.5, roughness, 0.0);
+ fragAlbedo = vec4(u_seaColor.rgb, 1.0);
#endif
}
#pragma end
@@ -97,6 +100,7 @@ void main() {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
@fragment fn fs_main(v : VSOut) -> FragOut {
#else
@@ -120,6 +124,7 @@ struct FragOut {
var frag : FragOut;
frag.color = vec4f(lit, mc.u_seaOpacity);
frag.surface = vec4f(0.5, 0.5, roughness, 0.0);
+ frag.albedo = vec4f(mc.u_seaColor.rgb, 1.0);
return frag;
#else
return vec4f(lit, mc.u_seaOpacity);
diff --git a/sdk/src/terrain/planetShader.ts b/sdk/src/terrain/planetShader.ts
index 95911969b..532aef555 100644
--- a/sdk/src/terrain/planetShader.ts
+++ b/sdk/src/terrain/planetShader.ts
@@ -213,6 +213,8 @@ in highp vec3 v_worldXYZ;
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -328,6 +330,7 @@ void main() {
fragColor = vec4(lit, 1.0);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(N), roughness, 0.75);
+ fragAlbedo = vec4(albedo, 1.0);
#endif
}
#pragma end
@@ -371,6 +374,7 @@ fn pnNoise(p : vec3f) -> f32 {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -463,6 +467,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(lit, 1.0);
frag.surface = vec4f(packNormal(N), roughness, 0.75);
+ frag.albedo = vec4f(albedo, 1.0);
return frag;
#else
return vec4f(lit, 1.0);
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.ts
index 4f5b70fd0..b04536a6c 100644
--- a/sdk/src/terrain/terrainShader.ts
+++ b/sdk/src/terrain/terrainShader.ts
@@ -160,6 +160,8 @@ in highp vec3 v_worldXYZ;
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -306,6 +308,7 @@ void main() {
fragColor = vec4(lit, 1.0);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(N), roughness, 0.75);
+ fragAlbedo = vec4(albedo, 1.0);
#endif
}
#pragma end
@@ -364,6 +367,7 @@ fn tnRead(layer : texture_2d<f32>, smp : sampler, p : vec2f, tile : f32, t : f32
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -465,6 +469,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(lit, 1.0);
frag.surface = vec4f(packNormal(Ns), roughness, 0.75);
+ frag.albedo = vec4f(albedo, 1.0);
return frag;
#else
return vec4f(lit, 1.0);
@@ -539,6 +544,8 @@ in highp vec3 v_worldXYZ;
#ifdef ES_GBUFFER
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -723,6 +730,7 @@ void main() {
fragColor = vec4(lit, 1.0);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(N), roughness, 0.75);
+ fragAlbedo = vec4(colour, 1.0);
#endif
}
#pragma end
@@ -798,6 +806,7 @@ fn tnBend(slope : vec2f, T0 : vec3f, B0 : vec3f, N : vec3f) -> vec3f {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -938,6 +947,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(lit, 1.0);
frag.surface = vec4f(packNormal(N), roughness, 0.75);
+ frag.albedo = vec4f(colour, 1.0);
return frag;
#else
return vec4f(lit, 1.0);
diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.ts
index de0aeb47a..d99add05d 100644
--- a/sdk/src/water/waterShader.ts
+++ b/sdk/src/water/waterShader.ts
@@ -334,6 +334,8 @@ vec2 packNormal(vec3 n) {
vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
return e * 0.5 + 0.5;
}
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
#else
out vec4 fragColor;
#endif
@@ -797,6 +799,7 @@ struct FragOut {
fn packNormal(n : vec3f) -> vec2f {
let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ fragAlbedo = vec4(0.0);
let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
var e = a.xy;
if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
@@ -806,6 +809,7 @@ fn packNormal(n : vec3f) -> vec2f {
fn waterHash(p0 : vec2f) -> f32 {
var p = fract(p0 * vec2f(0.1031, 0.1030));
+ @location(2) albedo : vec4f,
p = p + dot(p, p.yx + 33.33);
return fract((p.x + p.y) * p.x);
}
@@ -1254,3 +1258,4 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, amp : f32, calm : f32) -> vec
export const WATER_SHADER_TEMPLATE = makeBuiltinShaderTemplate('water', 'Water',
'Waves, refraction, reflection, depth absorption and foam for a horizontal body of water.', WATER);
+ frag.albedo = vec4f(0.0);
diff --git a/src/esengine/data/shaders/mesh.esshader b/src/esengine/data/shaders/mesh.esshader
index 24eb61375..40ab6db93 100644
--- a/src/esengine/data/shaders/mesh.esshader
+++ b/src/esengine/data/shaders/mesh.esshader
@@ -312,6 +312,8 @@ uniform sampler2D u_normalMap;
// would otherwise have to guess.
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+// The surface's diffuse colour, for light an effect adds after this pass (bounce light): alpha 1 is "written".
+layout(location = 2) out vec4 fragAlbedo;
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
@@ -371,6 +373,7 @@ void main() {
fragColor = vec4(shaded, base.a);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), 1.0, 0.75);
+ fragAlbedo = vec4(base.rgb, 1.0);
#endif
#else
#ifdef MESH_LIGHTMAP
@@ -640,6 +643,7 @@ struct VSOut {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -715,6 +719,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(shaded, base.a);
frag.surface = vec4f(packNormal(normalize(N)), 1.0, 0.75);
+ frag.albedo = vec4f(base.rgb, 1.0);
return frag;
#else
return vec4f(shaded, base.a);
diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cpp
index fb7ef0dc4..4651256c5 100644
--- a/src/esengine/renderer/draw/DrawList.cpp
+++ b/src/esengine/renderer/draw/DrawList.cpp
@@ -483,12 +483,14 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,
// record, written unblended where the device can say so per target.
desc.gbuffer = gbuffer_pass_ && materials.programWritesGBuffer(cmd.shader_id)
&& (!desc.blendEnabled || (composes && device.supportsUnblendedSurfaceTarget()));
+ desc.albedo = desc.gbuffer && materials.programWritesAlbedo(cmd.shader_id);
if (prepass) {
// Depth alone: the draw's depth program, unblended, no colour to either attachment. A cut-out keeps the alpha-to-coverage the colour pass draws with.
desc.program = ShaderHandle{cmd.depth_shader_id};
desc.blend = BlendMode::None;
desc.blendEnabled = false;
desc.gbuffer = false;
+ desc.albedo = false;
desc.colorWrite = false;
} else if (afterPrepass && takesPartInPrepass(cmd)) {
// Its depth is already in the buffer: it lands where it is level with it, and writes nothing there again.
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cpp
index 4da854e1d..b9c842d4c 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp
@@ -925,6 +925,11 @@ void PostProcessPipeline::renderPass(PostProcessPass& pass, const rg::PassContex
if (slot.name == "u_velocity" && velocity_active_ && velocity_fbo_) {
gpu = velocity_fbo_->getColorAttachment();
}
+ // The scene's albedo, beside its surface record: what bounce light an effect adds is coloured by.
+ if (slot.name == "u_sceneAlbedo" && graph_ && scene_gbuffer_) {
+ const TextureHandle albedo = graph_->albedoTextureOf(sceneResource_);
+ if (albedo != TextureHandle::Invalid) gpu = albedo;
+ }
if (slot.name == "u_rainCover" && cover_active_ && cover_fbo_) {
gpu = cover_fbo_->getColorAttachment();
}
diff --git a/src/esengine/renderer/frame/VoxelGI.cpp b/src/esengine/renderer/frame/VoxelGI.cpp
index e4830d4c3..eafa449f8 100644
--- a/src/esengine/renderer/frame/VoxelGI.cpp
+++ b/src/esengine/renderer/frame/VoxelGI.cpp
@@ -243,6 +243,7 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
const char* kApply = R"WGSL(
@group(0) @binding(4) var scene : texture_2d<f32>;
@group(0) @binding(5) var depthTex : texture_depth_2d;
+@group(0) @binding(6) var albedoTex : texture_2d<f32>;
@group(0) @binding(7) var outTex : texture_storage_2d<rgba16float, write>;
@group(0) @binding(8) var giTex : texture_2d<f32>;
@@ -277,9 +278,12 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
}
if (total > 1e-4) { light = acc / total; }
}
- // What the gathered light is lit onto wears the surface's own colour (its chroma, kept below white).
+ // What the gathered light is lit onto reflects it by its own diffuse colour, which the surface wrote beside its
+ // normal. A surface that wrote none (a shader older than the attachment) has its colour guessed from the picture.
+ let written = textureLoad(albedoTex, t, 0);
let lum = max(dot(src.rgb, vec3f(0.2126, 0.7152, 0.0722)), 0.02);
- let albedo = mix(vec3f(0.55), clamp(src.rgb / lum, vec3f(0.0), vec3f(1.6)) * 0.55, 0.7);
+ let guessed = mix(vec3f(0.55), clamp(src.rgb / lum, vec3f(0.0), vec3f(1.6)) * 0.55, 0.7);
+ let albedo = select(guessed, written.rgb, written.a > 0.5);
var result = src.rgb + albedo * light * gi.a.w;
if (gi.b.w > 0.5) { result = light * gi.a.w; }
textureStore(outTex, t, vec4f(result, src.a));
@@ -539,18 +543,21 @@ rg::ResourceId VoxelGI::declare(rg::RenderGraph& graph, rg::ResourceId input, rg
pass.write = result;
pass.execute = [this, &graph, scene, cur](const rg::PassContext& ctx) {
const TextureHandle depth = graph.depthTextureOf(scene);
- if (depth == TextureHandle::Invalid || ctx.output == TextureHandle::Invalid) return;
- const GfxComputeBinding bindings[8] = {
+ const TextureHandle albedo = graph.albedoTextureOf(scene);
+ if (depth == TextureHandle::Invalid || albedo == TextureHandle::Invalid
+ || ctx.output == TextureHandle::Invalid) return;
+ const GfxComputeBinding bindings[9] = {
GfxComputeBinding::uniform(0, context_.frameUbo()),
GfxComputeBinding::uniform(1, params_),
GfxComputeBinding::storage(2, grid_),
GfxComputeBinding::storage(3, tags_),
GfxComputeBinding::sampled(4, ctx.input(0)),
GfxComputeBinding::depth(5, depth),
+ GfxComputeBinding::sampled(6, albedo),
GfxComputeBinding::storageTexture(7, ctx.output),
GfxComputeBinding::sampled(8, history_[cur]),
};
- device_.dispatchCompute(apply_, bindings, 8, (ctx.width + 7) / 8, (ctx.height + 7) / 8);
+ device_.dispatchCompute(apply_, bindings, 9, (ctx.width + 7) / 8, (ctx.height + 7) / 8);
};
graph.addPass(std::move(pass));
}
diff --git a/src/esengine/renderer/graph/RenderGraph.cpp b/src/esengine/renderer/graph/RenderGraph.cpp
index f914de3eb..3ef678307 100644
--- a/src/esengine/renderer/graph/RenderGraph.cpp
+++ b/src/esengine/renderer/graph/RenderGraph.cpp
@@ -400,6 +400,13 @@ TextureHandle RenderGraph::gbufferTextureOf(ResourceId id) const {
return pool_.gbufferTextureOf(res.pooled);
}
+TextureHandle RenderGraph::albedoTextureOf(ResourceId id) const {
+ if (id >= resources_.size()) return TextureHandle::Invalid;
+ const Resource& res = resources_[id];
+ if (res.kind != Kind::Transient || res.pooled == kNoTarget) return TextureHandle::Invalid;
+ return pool_.albedoTextureOf(res.pooled);
+}
+
void RenderGraph::releasePool() {
for (auto& res : resources_) {
res.pooled = kNoTarget;
diff --git a/src/esengine/renderer/graph/RenderGraph.hpp b/src/esengine/renderer/graph/RenderGraph.hpp
index 54093cf76..2786e17a9 100644
--- a/src/esengine/renderer/graph/RenderGraph.hpp
+++ b/src/esengine/renderer/graph/RenderGraph.hpp
@@ -282,6 +282,8 @@ public:
/** Its second colour attachment, for a resource declared with one. Invalid otherwise. */
TextureHandle gbufferTextureOf(ResourceId id) const;
+ /// The third attachment of a gbuffer target: the surface's diffuse colour (alpha 1 where one was written).
+ TextureHandle albedoTextureOf(ResourceId id) const;
/** Drops every pooled target. For shutdown and for device loss, after which
* the handles the pool holds name nothing. */
diff --git a/src/esengine/renderer/graph/TargetPool.cpp b/src/esengine/renderer/graph/TargetPool.cpp
index da0df2cca..2a0f39453 100644
--- a/src/esengine/renderer/graph/TargetPool.cpp
+++ b/src/esengine/renderer/graph/TargetPool.cpp
@@ -135,6 +135,11 @@ TextureHandle TargetPool::gbufferTextureOf(TargetHandle handle) const {
return entry ? entry->fbo->getGBufferAttachment() : TextureHandle::Invalid;
}
+TextureHandle TargetPool::albedoTextureOf(TargetHandle handle) const {
+ const Entry* entry = resolve(handle);
+ return entry ? entry->fbo->getAlbedoAttachment() : TextureHandle::Invalid;
+}
+
void TargetPool::age() {
for (auto& entry : entries_) {
if (!entry.fbo || entry.inUse) continue;
diff --git a/src/esengine/renderer/graph/TargetPool.hpp b/src/esengine/renderer/graph/TargetPool.hpp
index 7eb2988ad..5dfa55c4a 100644
--- a/src/esengine/renderer/graph/TargetPool.hpp
+++ b/src/esengine/renderer/graph/TargetPool.hpp
@@ -89,6 +89,7 @@ public:
TextureHandle depthTextureOf(TargetHandle handle) const;
/** Its second colour attachment, Invalid for a target made without one. */
TextureHandle gbufferTextureOf(TargetHandle handle) const;
+ TextureHandle albedoTextureOf(TargetHandle handle) const;
/**
* @brief One tick of the clock every borrowed target is aged against.
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index db2e0fda3..77d629fa2 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -456,7 +456,7 @@ u32 MeshPlugin::meshProgram(RenderFrameContext& ctx, u32 variant) {
};
mesh_programs_[variant] = compile(features);
// Said to the store the draw list asks, so the pipeline of a draw on this program declares the second output.
- if (gbuffer && mesh_programs_[variant] != 0 && ctx.materials) ctx.materials->markGBufferProgram(mesh_programs_[variant]);
+ if (gbuffer && mesh_programs_[variant] != 0 && ctx.materials) ctx.materials->markGBufferProgram(mesh_programs_[variant], /*albedo=*/true);
return mesh_programs_[variant];
}
diff --git a/src/esengine/renderer/rhi/Framebuffer.cpp b/src/esengine/renderer/rhi/Framebuffer.cpp
index a6d4d1e36..6d0d1ac42 100644
--- a/src/esengine/renderer/rhi/Framebuffer.cpp
+++ b/src/esengine/renderer/rhi/Framebuffer.cpp
@@ -38,9 +38,13 @@ Framebuffer::Framebuffer(Framebuffer&& other) noexcept
resolveColor_(other.resolveColor_),
resolveDepth_(other.resolveDepth_),
colorAttachment1_(other.colorAttachment1_),
- resolveColor1_(other.resolveColor1_) {
+ resolveColor1_(other.resolveColor1_),
+ colorAttachment2_(other.colorAttachment2_),
+ resolveColor2_(other.resolveColor2_) {
other.colorAttachment1_ = TextureHandle::Invalid;
other.resolveColor1_ = TextureHandle::Invalid;
+ other.colorAttachment2_ = TextureHandle::Invalid;
+ other.resolveColor2_ = TextureHandle::Invalid;
other.handle_ = FramebufferHandle::Default;
other.colorAttachment_ = TextureHandle::Invalid;
other.depthAttachment_ = TextureHandle::Invalid;
@@ -60,8 +64,12 @@ Framebuffer& Framebuffer::operator=(Framebuffer&& other) noexcept {
resolveDepth_ = other.resolveDepth_;
colorAttachment1_ = other.colorAttachment1_;
resolveColor1_ = other.resolveColor1_;
+ colorAttachment2_ = other.colorAttachment2_;
+ resolveColor2_ = other.resolveColor2_;
other.colorAttachment1_ = TextureHandle::Invalid;
other.resolveColor1_ = TextureHandle::Invalid;
+ other.colorAttachment2_ = TextureHandle::Invalid;
+ other.resolveColor2_ = TextureHandle::Invalid;
other.handle_ = FramebufferHandle::Default;
other.colorAttachment_ = TextureHandle::Invalid;
other.depthAttachment_ = TextureHandle::Invalid;
@@ -144,9 +152,13 @@ bool Framebuffer::initialize() {
gDesc.minFilter = TextureFilter::Nearest;
gDesc.magFilter = TextureFilter::Nearest;
colorAttachment1_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
+ // Beside it, the surface's diffuse colour: light an effect adds after the frame lit it (bounce
+ // light) is coloured by what it lands on, and the lit picture cannot say that where the frame left it dark.
+ colorAttachment2_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
if (samples > 1) {
gDesc.samples = 1;
resolveColor1_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
+ resolveColor2_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
}
}
@@ -183,6 +195,8 @@ bool Framebuffer::initialize() {
fb.resolveDepthStencil = resolveDepth_;
fb.color1 = colorAttachment1_;
fb.resolveColor1 = resolveColor1_;
+ fb.color2 = colorAttachment2_;
+ fb.resolveColor2 = resolveColor2_;
fb.width = spec_.width;
fb.height = spec_.height;
handle_ = device_->createFramebuffer(fb);
@@ -210,6 +224,14 @@ void Framebuffer::cleanup() {
device_->deleteTexture(colorAttachment1_);
colorAttachment1_ = TextureHandle::Invalid;
}
+ if (resolveColor2_ != TextureHandle::Invalid) {
+ device_->deleteTexture(resolveColor2_);
+ resolveColor2_ = TextureHandle::Invalid;
+ }
+ if (colorAttachment2_ != TextureHandle::Invalid) {
+ device_->deleteTexture(colorAttachment2_);
+ colorAttachment2_ = TextureHandle::Invalid;
+ }
if (resolveDepth_ != TextureHandle::Invalid) {
device_->deleteTexture(resolveDepth_);
resolveDepth_ = TextureHandle::Invalid;
diff --git a/src/esengine/renderer/rhi/Framebuffer.hpp b/src/esengine/renderer/rhi/Framebuffer.hpp
index 8a9d2ca30..c35fbc575 100644
--- a/src/esengine/renderer/rhi/Framebuffer.hpp
+++ b/src/esengine/renderer/rhi/Framebuffer.hpp
@@ -159,6 +159,11 @@ public:
return resolveColor1_ != TextureHandle::Invalid ? resolveColor1_ : colorAttachment1_;
}
+ /** @brief The third colour attachment, the surface's diffuse colour (RGBA8, alpha 1 where a surface wrote it). */
+ TextureHandle getAlbedoAttachment() const {
+ return resolveColor2_ != TextureHandle::Invalid ? resolveColor2_ : colorAttachment2_;
+ }
+
/** @brief Gets the depth attachment texture (Invalid if none) */
TextureHandle getDepthAttachment() const {
return resolveDepth_ != TextureHandle::Invalid ? resolveDepth_ : depthAttachment_;
@@ -199,6 +204,9 @@ private:
/// The second colour attachment and its single-sample twin (spec_.gbuffer).
TextureHandle colorAttachment1_ = TextureHandle::Invalid;
TextureHandle resolveColor1_ = TextureHandle::Invalid;
+ /// The third, the surface's diffuse colour, and its twin (also spec_.gbuffer).
+ TextureHandle colorAttachment2_ = TextureHandle::Invalid;
+ TextureHandle resolveColor2_ = TextureHandle::Invalid;
};
} // namespace esengine
diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cpp
index ae530bee3..bcb785d76 100644
--- a/src/esengine/renderer/rhi/GLDevice.cpp
+++ b/src/esengine/renderer/rhi/GLDevice.cpp
@@ -234,7 +234,8 @@ EM_JS(void, es_gl_surface_blend, (int off), {
var ctx = GLctx;
var ext = ctx && (ctx.esIndexedBlend || (ctx.esIndexedBlend = ctx.getExtension('OES_draw_buffers_indexed')));
if (!ext) return;
- if (off) ext.disableiOES(ctx.BLEND, 1); else ext.enableiOES(ctx.BLEND, 1);
+ if (off) { ext.disableiOES(ctx.BLEND, 1); ext.disableiOES(ctx.BLEND, 2); }
+ else { ext.enableiOES(ctx.BLEND, 1); ext.enableiOES(ctx.BLEND, 2); }
});
#endif
@@ -1206,7 +1207,7 @@ void GLDevice::backendSetPipeline(u32 id, const PipelineDesc& desc) {
alpha_to_coverage_ = static_cast<i8>(desc.alphaToCoverage);
}
- if (current_fbo_has_gbuffer_) applyDrawBuffers(desc.gbuffer);
+ if (current_fbo_has_gbuffer_) applyDrawBuffers(desc.gbuffer, desc.albedo);
applySurfaceBlend(desc.blendEnabled && desc.gbuffer && current_fbo_has_gbuffer_);
current_pipeline_id_ = id;
@@ -1519,6 +1520,7 @@ bool GLDevice::backendCreateFramebuffer(u32 id, const FramebufferDesc& desc) {
if (desc.color0 != TextureHandle::Invalid) attach(GL_COLOR_ATTACHMENT0, desc.color0);
if (desc.color1 != TextureHandle::Invalid) attach(GL_COLOR_ATTACHMENT1, desc.color1);
+ if (desc.color2 != TextureHandle::Invalid) attach(GL_COLOR_ATTACHMENT2, desc.color2);
if (desc.depthStencil != TextureHandle::Invalid) {
// Attach point follows the texture's pixel format (depth-only vs packed depth+stencil).
attach(depthPoint(desc.depthStencil), desc.depthStencil);
@@ -1541,6 +1543,7 @@ bool GLDevice::backendCreateFramebuffer(u32 id, const FramebufferDesc& desc) {
glBindFramebuffer(GL_FRAMEBUFFER, dst);
attach(GL_COLOR_ATTACHMENT0, desc.resolveColor0);
if (desc.resolveColor1 != TextureHandle::Invalid) attach(GL_COLOR_ATTACHMENT1, desc.resolveColor1);
+ if (desc.resolveColor2 != TextureHandle::Invalid) attach(GL_COLOR_ATTACHMENT2, desc.resolveColor2);
if (desc.resolveDepthStencil != TextureHandle::Invalid) {
attach(depthPoint(desc.resolveDepthStencil), desc.resolveDepthStencil);
}
@@ -1549,7 +1552,8 @@ bool GLDevice::backendCreateFramebuffer(u32 id, const FramebufferDesc& desc) {
if (ok) {
framebuffer_resolve_[id] = {dst, desc.width, desc.height,
desc.resolveDepthStencil != TextureHandle::Invalid,
- desc.resolveColor1 != TextureHandle::Invalid};
+ desc.resolveColor1 != TextureHandle::Invalid,
+ desc.resolveColor2 != TextureHandle::Invalid};
} else {
glDeleteFramebuffers(1, &dst);
ES_LOG_ERROR("GLDevice: multisample resolve target incomplete");
@@ -1575,14 +1579,21 @@ void GLDevice::resolveFramebuffer(u32 framebufferId) {
if (r.gbuffer) {
// Each attachment on its own: a blit copies the read buffer into every live draw buffer,
// and the two are different surfaces.
- const GLenum only0[2] = {GL_COLOR_ATTACHMENT0, GL_NONE};
- const GLenum only1[2] = {GL_NONE, GL_COLOR_ATTACHMENT1};
- glDrawBuffers(2, only0);
+ const GLsizei count = r.albedo ? 3 : 2;
+ const GLenum only0[3] = {GL_COLOR_ATTACHMENT0, GL_NONE, GL_NONE};
+ const GLenum only1[3] = {GL_NONE, GL_COLOR_ATTACHMENT1, GL_NONE};
+ const GLenum only2[3] = {GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT2};
+ glDrawBuffers(count, only0);
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT1);
- glDrawBuffers(2, only1);
+ glDrawBuffers(count, only1);
// Nearest: a normal or a roughness averaged with its neighbour's is neither's.
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
+ if (r.albedo) {
+ glReadBuffer(GL_COLOR_ATTACHMENT2);
+ glDrawBuffers(count, only2);
+ glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
+ }
glReadBuffer(GL_COLOR_ATTACHMENT0);
} else {
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_LINEAR);
@@ -1594,10 +1605,11 @@ void GLDevice::resolveFramebuffer(u32 framebufferId) {
}
if (pass_discard_msaa_) {
// Resolved, and nothing draws on it again: a tile-based GPU need not write the samples out to memory.
- GLenum attachments[4];
+ GLenum attachments[5];
GLsizei n = 0;
attachments[n++] = GL_COLOR_ATTACHMENT0;
if (r.gbuffer) attachments[n++] = GL_COLOR_ATTACHMENT1;
+ if (r.albedo) attachments[n++] = GL_COLOR_ATTACHMENT2;
attachments[n++] = GL_DEPTH_ATTACHMENT;
attachments[n++] = GL_STENCIL_ATTACHMENT;
glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, n, attachments);
@@ -1671,6 +1683,9 @@ void GLDevice::beginRenderPass(const RenderPassDesc& desc) {
if (fb->color1 != TextureHandle::Invalid) {
evictSamplerBinding(nameOf(texture_names_, static_cast<u32>(fb->color1)));
}
+ if (fb->color2 != TextureHandle::Invalid) {
+ evictSamplerBinding(nameOf(texture_names_, static_cast<u32>(fb->color2)));
+ }
}
// Which draw buffers are live is the framebuffer's own state and a pipeline decides it per draw,
// so a pass starts by saying it is not yet known — and the pipeline cache forgets, or a pipeline
@@ -1682,6 +1697,7 @@ void GLDevice::beginRenderPass(const RenderPassDesc& desc) {
draw_buffers_ = -1;
}
current_fbo_has_gbuffer_ = hasGBuffer;
+ current_fbo_has_albedo_ = hasGBuffer && passTarget->color2 != TextureHandle::Invalid;
if (!desc.clearColor && !desc.clearDepth && !desc.clearStencil) return;
@@ -1707,12 +1723,13 @@ void GLDevice::beginRenderPass(const RenderPassDesc& desc) {
}
// The first attachment takes the clear colour (the camera's background); the second records what a
// surface is, and the background is not one, so it clears to zero — "nothing wrote here".
- if (hasGBuffer && desc.clearColor) applyDrawBuffers(false);
+ if (hasGBuffer && desc.clearColor) applyDrawBuffers(false, false);
clear(desc.clearColor, desc.clearDepth, desc.clearStencil);
if (hasGBuffer && desc.clearColor) {
- applyDrawBuffers(true);
+ applyDrawBuffers(true, current_fbo_has_albedo_);
const GLfloat zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
glClearBufferfv(GL_COLOR, 1, zero);
+ if (current_fbo_has_albedo_) glClearBufferfv(GL_COLOR, 2, zero);
}
if (scoped) setScissorTest(false);
}
@@ -1725,14 +1742,17 @@ void GLDevice::applySurfaceBlend(bool off) {
surface_blend_off_ = off;
}
-void GLDevice::applyDrawBuffers(bool gbuffer) {
- const i8 want = gbuffer ? 1 : 0;
+void GLDevice::applyDrawBuffers(bool gbuffer, bool albedo) {
+ const i8 want = !gbuffer ? 0 : (albedo && current_fbo_has_albedo_) ? 2 : 1;
if (draw_buffers_ == want) return;
- // The second slot is NONE for a draw that does not write it, so a shader with one output does not
- // leave an undefined value in the surface record.
- const GLenum both[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
- const GLenum first[2] = {GL_COLOR_ATTACHMENT0, GL_NONE};
- glDrawBuffers(2, gbuffer ? both : first);
+ // A slot is NONE for a draw that does not write it, so a shader with fewer outputs does not leave an
+ // undefined value in the surface record or the albedo.
+ const GLenum sets[3][3] = {
+ {GL_COLOR_ATTACHMENT0, GL_NONE, GL_NONE},
+ {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_NONE},
+ {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2},
+ };
+ glDrawBuffers(current_fbo_has_albedo_ ? 3 : 2, sets[want]);
draw_buffers_ = want;
}
@@ -1743,6 +1763,7 @@ void GLDevice::endRenderPass() {
pass_discard_depth_ = false;
current_framebuffer_ = 0;
current_fbo_has_gbuffer_ = false;
+ current_fbo_has_albedo_ = false;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
diff --git a/src/esengine/renderer/rhi/GLDevice.hpp b/src/esengine/renderer/rhi/GLDevice.hpp
index e08bbd475..2a53100e2 100644
--- a/src/esengine/renderer/rhi/GLDevice.hpp
+++ b/src/esengine/renderer/rhi/GLDevice.hpp
@@ -274,11 +274,13 @@ private:
i16 current_depth_bias_ = 0;
i8 depth_equal_ = -1; ///< -1: unknown. 1 when the depth test passes on equal depth too.
i8 alpha_to_coverage_ = -1; ///< -1: unknown, so the next pipeline sets it.
- /// The framebuffer being drawn into carries a second colour attachment, and which of the two draw
- /// buffer sets is live: 0 = colour only, 1 = both, -1 = not yet said for this pass.
+ /// The framebuffer being drawn into carries a second colour attachment (and a third, the albedo, beside
+ /// it), and which draw buffer set is live: 0 = colour only, 1 = colour and surface, 2 = all three,
+ /// -1 = not yet said for this pass.
bool current_fbo_has_gbuffer_ = false;
+ bool current_fbo_has_albedo_ = false;
i8 draw_buffers_ = -1;
- void applyDrawBuffers(bool gbuffer);
+ void applyDrawBuffers(bool gbuffer, bool albedo);
/// OES_draw_buffers_indexed: 1 enabled, 0 absent (asked once at init).
i8 indexed_blend_ = 0;
/// Blending is off on the second draw buffer while the first blends (a composing draw writing the surface record).
@@ -329,6 +331,7 @@ private:
u32 height = 0;
bool depth = false;
bool gbuffer = false;
+ bool albedo = false;
};
std::unordered_map<u32, ResolvePair> framebuffer_resolve_;
/// Blit a multisampled target into the single-sample one it owns; no-op for
diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hpp
index 9f6eee11f..1c24f0e66 100644
--- a/src/esengine/renderer/rhi/GfxEnums.hpp
+++ b/src/esengine/renderer/rhi/GfxEnums.hpp
@@ -444,6 +444,10 @@ struct FramebufferDesc {
TextureHandle color1 = TextureHandle::Invalid;
/// Its single-sample twin, when the target is multisampled.
TextureHandle resolveColor1 = TextureHandle::Invalid;
+ /// A third, beside the second whenever it is there: the surface's diffuse colour, for light an effect adds
+ /// after the frame (bounce light), which must be coloured by what it lands on. See `PipelineDesc::albedo`.
+ TextureHandle color2 = TextureHandle::Invalid;
+ TextureHandle resolveColor2 = TextureHandle::Invalid;
/// Pixel size, required only when resolving: a blit needs a rectangle and an
/// attachment handle carries no dimensions.
u32 width = 0;
diff --git a/src/esengine/renderer/rhi/PipelineState.hpp b/src/esengine/renderer/rhi/PipelineState.hpp
index 6de07b1f9..06d64dd9e 100644
--- a/src/esengine/renderer/rhi/PipelineState.hpp
+++ b/src/esengine/renderer/rhi/PipelineState.hpp
@@ -80,6 +80,9 @@ struct PipelineDesc {
/// to receive it. A pipeline without it draws into a target that has one and leaves it alone:
/// WebGPU wants that said in the pipeline, and GL wants the draw buffers set to match.
bool gbuffer = false;
+ /// With `gbuffer`: the shader also writes the surface's diffuse colour (location 2) into the target's
+ /// third attachment. A gbuffer shader that predates it writes two outputs and leaves the third alone.
+ bool albedo = false;
/// False writes no colour, to either attachment: a depth-only pass, whose surfaces are then drawn again with colour.
bool colorWrite = true;
/// Passes where the depth is EQUAL to what the buffer holds, not only where it is less: a surface drawn a second time, after a pass that
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index 55ccc7958..cb803c24a 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -858,6 +858,8 @@ uniform sampler2D u_normalMap;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragSurface;
+layout(location = 2) out vec4 fragAlbedo;
+
vec2 packNormal(vec3 n) {
n /= (abs(n.x) + abs(n.y) + abs(n.z));
vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
@@ -916,6 +918,7 @@ void main() {
fragColor = vec4(shaded, base.a);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), 1.0, 0.75);
+ fragAlbedo = vec4(base.rgb, 1.0);
#endif
#else
#ifdef MESH_LIGHTMAP
@@ -1087,11 +1090,11 @@ fn esPrevBone(joint : u32) -> mat4x4f {
+ v.a_weights.z * esPrevBone(v.a_joints.z) + v.a_weights.w * esPrevBone(v.a_joints.w);
let L = prevSkin * vec4f(local, 1.0);
#else
- let L = vec4f(local, 1.0);
+)esshader"
+ R"esshader( let L = vec4f(local, 1.0);
#endif
let before = esInstanceTint();
-)esshader"
- R"esshader( out.v_velCur = out.pos;
+ out.v_velCur = out.pos;
let prev = vec4f(dot(esInstanceTexel(4), L), dot(esInstanceTexel(5), L),
dot(esInstanceTexel(6), L), dot(esInstanceTexel(7), L));
out.v_velPrev = vec4f(prev.xy + before.xy * prev.w, prev.zw);
@@ -1186,6 +1189,7 @@ struct VSOut {
struct FragOut {
@location(0) color : vec4f,
@location(1) surface : vec4f,
+ @location(2) albedo : vec4f,
};
fn packNormal(n : vec3f) -> vec2f {
@@ -1261,6 +1265,7 @@ fn packNormal(n : vec3f) -> vec2f {
var frag : FragOut;
frag.color = vec4f(shaded, base.a);
frag.surface = vec4f(packNormal(normalize(N)), 1.0, 0.75);
+ frag.albedo = vec4f(base.rgb, 1.0);
return frag;
#else
return vec4f(shaded, base.a);
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index 5db437d8c..8873cdd33 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -72,10 +72,12 @@ u32 MaterialStore::meshProgram(u32 materialId, resource::ResourceManager& resour
bool lightmapped, bool gbuffer, bool tangents, bool tipPass) const {
// Only for a shader that has something to write: one that never mentions the feature would
// compile the same program twice and then be declared to write an output it does not have.
+ bool albedo = false;
if (gbuffer) {
const MaterialRecord* rec = find(materialId);
const auto src = rec && rec->shaderRef.isValid() ? sources_.find(rec->shaderRef.id()) : sources_.end();
gbuffer = src != sources_.end() && src->second.mentionsGbuffer;
+ albedo = gbuffer && src->second.writesAlbedo;
}
// Keyed by shader AND vertex shape: a layout may not declare an attribute its
// shader ignores, so geometry with normals — or posed by bones — needs its own.
@@ -93,7 +95,7 @@ u32 MaterialStore::meshProgram(u32 materialId, resource::ResourceManager& resour
if (const auto hit = bucket->second.find(shape); hit != bucket->second.end()) {
const Shader* shader = resources.getShader(hit->second);
const u32 cached = shader ? shader->getProgramId() : 0;
- if (gbuffer && cached != 0) gbuffer_programs_.insert(cached);
+ if (gbuffer && cached != 0) markGBufferProgram(cached, albedo);
return cached;
}
}
@@ -114,7 +116,7 @@ u32 MaterialStore::meshProgram(u32 materialId, resource::ResourceManager& resour
if (gbuffer) features.push_back("ES_GBUFFER");
if (tipPass) features.push_back("ES_TIP_PASS");
const u32 program = variantProgram(materialId, resources, shape, features, "resident geometry");
- if (gbuffer && program != 0) gbuffer_programs_.insert(program);
+ if (gbuffer && program != 0) markGBufferProgram(program, albedo);
return program;
}
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index e28dfe88f..f0d47ddb8 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -200,7 +200,8 @@ public:
void rememberSource(resource::ShaderHandle shader, std::string source, std::string features) {
if (shader.isValid()) {
const bool mentionsGbuffer = source.find("ES_GBUFFER") != std::string::npos;
- sources_[shader.id()] = { std::move(source), std::move(features), mentionsGbuffer };
+ const bool writesAlbedo = mentionsGbuffer && source.find("fragAlbedo") != std::string::npos;
+ sources_[shader.id()] = { std::move(source), std::move(features), mentionsGbuffer, writesAlbedo };
}
}
@@ -218,8 +219,15 @@ public:
bool programWritesGBuffer(u32 programId) const {
return gbuffer_programs_.count(programId) != 0;
}
+ /// Of those, the ones that also write the surface's diffuse colour (location 2, `fragAlbedo`).
+ bool programWritesAlbedo(u32 programId) const {
+ return albedo_programs_.count(programId) != 0;
+ }
/// A program that is not a material's — the engine's own mesh shader — says it writes the record.
- void markGBufferProgram(u32 programId) const { gbuffer_programs_.insert(programId); }
+ void markGBufferProgram(u32 programId, bool albedo) const {
+ gbuffer_programs_.insert(programId);
+ if (albedo) albedo_programs_.insert(programId);
+ }
/// The name of the shader @p materialId draws with, or "default" for the path's own batch shader.
const std::string& shaderLabel(u32 materialId) const {
@@ -452,6 +460,9 @@ private:
/// Whether the source mentions ES_GBUFFER, found once when it is stored: the mesh path asks it for every draw, and the answer was a
/// substring search through the whole text each time.
bool mentionsGbuffer = false;
+ /// Whether its gbuffer variant also writes `fragAlbedo` (location 2): a shader written before the
+ /// attachment existed writes two outputs, and its pipeline must leave the third alone.
+ bool writesAlbedo = false;
};
std::unordered_map<u32, ShaderSource> sources_;
/// (Handle, vertex source) → the program compiled for it (0 = tried and
@@ -462,6 +473,8 @@ private:
mutable std::unordered_map<u32, std::unordered_map<u64, resource::ShaderHandle>> mesh_programs_;
/// Programs compiled with ES_GBUFFER (see programWritesGBuffer).
mutable std::unordered_set<u32> gbuffer_programs_;
+ /// Of those, the ones that write `fragAlbedo` too (see programWritesAlbedo).
+ mutable std::unordered_set<u32> albedo_programs_;
TextureHandle sceneColor_ = TextureHandle::Invalid;
TextureHandle sceneDepth_ = TextureHandle::Invalid;
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index 4b16e9361..47828d8ab 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -1295,7 +1295,9 @@ WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {
PipelineRec& built = pipelines_[id];
const u32 dsVariant = dsVariantOf(pass_ds_format_);
const PipelineDesc& desc = *described;
+ const bool writesAlbedo = pass_albedo_ && desc.gbuffer && desc.albedo;
const u32 gbufferVariant = !pass_gbuffer_ ? kGBufferNone
+ : writesAlbedo ? kGBufferWritesAlbedo
: desc.gbuffer ? kGBufferWrites : kGBufferSkips;
const u32 variant = ((dsVariant * kColorVariantCount + colorVariantOf(pass_color_format_))
* kSampleVariantCount + (pass_samples_ > 1 ? kMultiSample : kSingleSample))
@@ -1339,7 +1341,7 @@ WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {
blendState.color = blend.color;
blendState.alpha = blend.alpha;
- WGPUColorTargetState targets[2]{};
+ WGPUColorTargetState targets[3]{};
targets[0].format = pass_color_format_;
targets[0].blend = desc.blendEnabled ? &blendState : nullptr;
targets[0].writeMask = (writesStencil(desc.stencil) || !desc.colorWrite) ? WGPUColorWriteMask_None
@@ -1351,11 +1353,15 @@ WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {
targets[1].format = WGPUTextureFormat_RGBA8Unorm;
targets[1].writeMask = (desc.gbuffer && desc.colorWrite && !writesStencil(desc.stencil)) ? WGPUColorWriteMask_All
: WGPUColorWriteMask_None;
+ // The third, the albedo, the same way: written only by a shader that has a location-2 output.
+ targets[2].format = WGPUTextureFormat_RGBA8Unorm;
+ targets[2].writeMask = (writesAlbedo && desc.colorWrite && !writesStencil(desc.stencil)) ? WGPUColorWriteMask_All
+ : WGPUColorWriteMask_None;
WGPUFragmentState fragment{};
fragment.module = progIt->second.fragment;
fragment.entryPoint = sv("fs_main");
- fragment.targetCount = pass_gbuffer_ ? 2 : 1;
+ fragment.targetCount = !pass_gbuffer_ ? 1 : pass_albedo_ ? 3 : 2;
fragment.targets = targets;
WGPURenderPipelineDescriptor pd{};
@@ -1732,7 +1738,7 @@ void WebGPUDevice::resolveDepthAttachment(u32 msaaDepth, u32 resolveDepth) {
WGPURenderPipeline WebGPUDevice::ensureClearPipeline(bool color, bool depth, bool stencil) {
// The sample count is part of the key for the same reason the formats are:
// a clear pipeline is validated against the pass it runs in.
- const u32 key = (pass_gbuffer_ ? 1u << 12 : 0u) |
+ const u32 key = (pass_gbuffer_ ? 1u << 12 : 0u) | (pass_albedo_ ? 1u << 13 : 0u) |
colorVariantOf(pass_color_format_) << 6 |
dsVariantOf(pass_ds_format_) << 4 |
(pass_samples_ > 1 ? 8u : 0u) |
@@ -1794,17 +1800,19 @@ struct ClearColor { value : vec4f };
WGPUShaderModule vs = makeModule(kClearVS);
WGPUShaderModule fs = makeModule(kClearFS);
- WGPUColorTargetState clearTargets[2]{};
+ WGPUColorTargetState clearTargets[3]{};
clearTargets[0].format = pass_color_format_;
clearTargets[0].writeMask = color ? WGPUColorWriteMask_All : WGPUColorWriteMask_None;
// A scoped clear is the camera's background, which a surface record has no part in.
clearTargets[1].format = WGPUTextureFormat_RGBA8Unorm;
clearTargets[1].writeMask = WGPUColorWriteMask_None;
+ clearTargets[2].format = WGPUTextureFormat_RGBA8Unorm;
+ clearTargets[2].writeMask = WGPUColorWriteMask_None;
WGPUFragmentState fragment{};
fragment.module = fs;
fragment.entryPoint = sv("fs_main");
- fragment.targetCount = pass_gbuffer_ ? 2 : 1;
+ fragment.targetCount = !pass_gbuffer_ ? 1 : pass_albedo_ ? 3 : 2;
fragment.targets = clearTargets;
WGPURenderPipelineDescriptor pd{};
@@ -2209,8 +2217,11 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
pass_is_surface_ = desc.target == FramebufferHandle::Default;
pass_samples_ = 1;
pass_gbuffer_ = false;
+ pass_albedo_ = false;
WGPUTextureView gbufferView = nullptr;
WGPUTextureView gbufferResolveView = nullptr;
+ WGPUTextureView albedoView = nullptr;
+ WGPUTextureView albedoResolveView = nullptr;
pass_depth_msaa_ = 0;
pass_depth_resolve_ = 0;
if (desc.target != FramebufferHandle::Default) {
@@ -2260,6 +2271,21 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
}
}
}
+ if (pass_gbuffer_ && fb->color2 != TextureHandle::Invalid) {
+ auto aIt = textures_.find(static_cast<u32>(fb->color2));
+ if (aIt != textures_.end()) {
+ albedoView = aIt->second.view;
+ pass_albedo_ = true;
+ if (aIt->second.samples > 1) {
+ auto aResolve = textures_.find(static_cast<u32>(fb->resolveColor2));
+ if (aResolve == textures_.end()) {
+ ES_LOG_ERROR("WebGPUDevice::beginRenderPass: a multisampled albedo with no resolve twin");
+ return;
+ }
+ albedoResolveView = aResolve->second.view;
+ }
+ }
+ }
if (depthStencil != 0) {
auto dsIt = textures_.find(depthStencil);
if (dsIt != textures_.end()) {
@@ -2362,7 +2388,7 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
color.clearValue = toWGPUClearColor(desc);
// The surface record clears to zero — "nothing wrote here" — whatever the background is.
- WGPURenderPassColorAttachment colors[2]{};
+ WGPURenderPassColorAttachment colors[3]{};
colors[0] = color;
if (pass_gbuffer_) {
colors[1].view = gbufferView;
@@ -2372,9 +2398,17 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
colors[1].storeOp = (desc.discardMultisampled && gbufferResolveView) ? WGPUStoreOp_Discard : WGPUStoreOp_Store;
colors[1].clearValue = {0.0, 0.0, 0.0, 0.0};
}
+ if (pass_albedo_) {
+ colors[2].view = albedoView;
+ colors[2].resolveTarget = albedoResolveView;
+ colors[2].depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
+ colors[2].loadOp = toWGPULoadOp(desc.clearColor, scoped);
+ colors[2].storeOp = (desc.discardMultisampled && albedoResolveView) ? WGPUStoreOp_Discard : WGPUStoreOp_Store;
+ colors[2].clearValue = {0.0, 0.0, 0.0, 0.0};
+ }
WGPURenderPassDescriptor rp{};
- rp.colorAttachmentCount = pass_gbuffer_ ? 2 : 1;
+ rp.colorAttachmentCount = !pass_gbuffer_ ? 1 : pass_albedo_ ? 3 : 2;
rp.colorAttachments = colors;
WGPURenderPassDepthStencilAttachment ds{};
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index 47eb11590..4cfeb696e 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -400,7 +400,7 @@ private:
};
/// What a pass's second colour attachment means to the pipeline drawn into it: there is none, the
/// pipeline writes it, or the pipeline leaves it alone (its target is declared with an empty write mask).
- enum GBufferVariant : u32 { kGBufferNone = 0, kGBufferWrites = 1, kGBufferSkips = 2, kGBufferVariantCount = 3 };
+ enum GBufferVariant : u32 { kGBufferNone = 0, kGBufferWrites = 1, kGBufferSkips = 2, kGBufferWritesAlbedo = 3, kGBufferVariantCount = 4 };
struct PipelineRec {
/// Lazily built per pass shape:
/// [((ds * kColorVariantCount + color) * kSampleVariantCount + samples) * kGBufferVariantCount + gbuffer].
@@ -555,6 +555,8 @@ private:
u32 pass_samples_ = 1;
/// The pass draws into a second colour attachment (always RGBA8) beside the first.
bool pass_gbuffer_ = false;
+ /// The pass also carries the third attachment, the surface's diffuse colour (FramebufferDesc::color2).
+ bool pass_albedo_ = false;
/// The multisampled depth attachment of the pass being ended, and the
/// single-sample texture it owes its contents to. Both 0 unless resolving.
u32 pass_depth_msaa_ = 0;
diff --git a/tools/render-lab/README.md b/tools/render-lab/README.md
index eb840cd33..0483dd575 100644
--- a/tools/render-lab/README.md
+++ b/tools/render-lab/README.md
@@ -56,7 +56,12 @@ Each figure is a region's linear value in the engine as a share of Cycles' value
- **The bake is right once it bounces enough.** With 8 bounces it lands within 5–18% of Cycles, and with 12 within 4–10%. Two bounces, which was the default, left a light-walled room at about half its light. The default is 8 now, and the inspector goes to 16. More bounces cost little: a 252k-lumel bake took 1.7 s at 2 bounces and 2.4 s at 8.
- **The real-time methods give a fraction of the bounce.** Voxel GI gives 0.04–0.15 of it, SSGI about none. Faces that only a later bounce reaches get nothing.
- **Nothing off screen.** Both real-time methods seed from the frame's own picture, so `gi_away` is black under them. A surface cache lit independently of the view is what changes this.
-- **No colour bleed.** On the red wall the real-time red channel is 0.11–0.15 of Cycles while green and blue are 0.5–0.85: the bounce arrives grey.
+- **No colour bleed, until the albedo attachment.** On the red wall the real-time red channel was 0.11–0.15 of Cycles while green and blue were 0.5–0.85, so the bounce arrived grey. Both effects guessed the receiver's colour from the lit picture, which is black where the sun does not reach. Since the scene pass writes the albedo beside the normal, Voxel GI gives:
+ - WebGL2: ceiling 0.72–0.90, red wall 0.75/0.73/0.77, back wall 0.32–0.45.
+ - WebGPU compute: ceiling 0.57–0.71, red wall 0.48–0.57.
+ - Emissive room: ceiling 0.44–0.52 (SSGI 0.36–0.43).
+
+ Still about 0: the floor in shade and the block's face, which need a second bounce, and `gi_away`. Render gates `gi-room-baked` and `gi-room-voxel` hold the bake and the albedo path.
- **No leak in `gi_leak`.** That is only because the lit outside is never on screen.
- **Banding.** The WebGL2 voxel fallback and SSGI band visibly in the emissive room.
- **Built-in meshes did not draw their bake until d39f6842e.** They had no `TexCoord1` at runtime.
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 6c8e62b66..97c559a13 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -160,6 +160,13 @@ export const SCENES = [
// The bake through an imported model's material under an environment: it replaces the
// environment's diffuse half. The quad carries a normal and the material a tint, so a
// program that fails to link (seventeen samplers with the batch stream's eight) shows.
+ // A room of built-in cubes lit by a sun through a skylight and baked (8 bounces): the walls are bounce light alone, so a bake the frame
+ // never reads (a built-in mesh with no lightmap UV set) leaves them black, and too few bounces leave them at half. Probe colours are the
+ // engine's, each within 4 of Cycles' own render of the same room (tools/render-lab, gi_room).
+ { id: "gi-room-baked", tier: "pr", webgpu: true, env: { "ESTELLA_VERIFY_COLORSPACE": "linear", "ESTELLA_VERIFY_SCENE": "/scenes/gi-room-baked.esscene", "ESTELLA_VERIFY_MANIFEST": "/scenes/gi-room-baked.textures.json", "ESTELLA_VERIFY_TEXTURES": "runtime", "ESTELLA_VERIFY_W": "320", "ESTELLA_VERIFY_H": "180", "ESTELLA_VERIFY_STEPS": "4", "ESTELLA_VERIFY_EXPECT": "[{\"x\":0.5,\"y\":0.099,\"rgb\":[66,63,60],\"tol\":10},{\"x\":0.239,\"y\":0.508,\"rgb\":[69,30,26],\"tol\":10},{\"x\":0.761,\"y\":0.508,\"rgb\":[23,46,21],\"tol\":10},{\"x\":0.441,\"y\":0.344,\"rgb\":[61,55,53],\"tol\":10},{\"x\":0.375,\"y\":0.711,\"rgb\":[64,54,51],\"tol\":10}]" } },
+ // The same room unbaked, lit by Voxel GI: the bounce light on the red wall is red, because it is reflected by the albedo the wall wrote beside its
+ // normal. Before the albedo attachment it was guessed from the lit picture, and a wall the sun does not reach came out grey.
+ { id: "gi-room-voxel", tier: "pr", webgpu: true, env: { "ESTELLA_VERIFY_PLAY": "1", "ESTELLA_VERIFY_COLORSPACE": "linear", "ESTELLA_VERIFY_SCENE": "/scenes/gi-room-voxel.esscene", "ESTELLA_VERIFY_MANIFEST": "/scenes/gi-room-voxel.textures.json", "ESTELLA_VERIFY_W": "320", "ESTELLA_VERIFY_H": "180", "ESTELLA_VERIFY_STEPS": "120", "ESTELLA_VERIFY_EXPECT": "[{\"x\":0.5,\"y\":0.099,\"rgb\":[58,58,58],\"tol\":16},{\"x\":0.239,\"y\":0.508,\"rgb\":[64,28,25],\"tol\":16},{\"x\":0.761,\"y\":0.508,\"rgb\":[18,41,20],\"tol\":16}]" } },
{ id: "lightmap-model", tier: "pr", webgpu: true, env: { "ESTELLA_VERIFY_MESH_MATERIAL": "/scenes/lightmap-model/rough.esmaterial", "ESTELLA_VERIFY_SCENE": "/scenes/lightmap-model.esscene", "ESTELLA_VERIFY_MANIFEST": "/scenes/lightmap-model.textures.json", "ESTELLA_VERIFY_W": "256", "ESTELLA_VERIFY_H": "256", "ESTELLA_VERIFY_STEPS": "2", "ESTELLA_VERIFY_EXPECT": "[{\"x\":0.25,\"y\":0.4,\"rgb\":[0,255,255],\"tol\":20},{\"x\":0.25,\"y\":0.6,\"rgb\":[128,255,0],\"tol\":20},{\"x\":0.75,\"y\":0.4,\"rgb\":[29,255,61],\"tol\":20},{\"x\":0.75,\"y\":0.6,\"rgb\":[29,255,61],\"tol\":20}]" } },
// Why the rectangle rides the per-object record: ONE mesh and ONE atlas, and
// the two objects drawn from them read different halves of it. A rectangle