@tinypoly/gpu-renderer 0.1.0 → 0.2.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/ARCHITECTURE.md +31 -3
- package/README.md +13 -4
- package/dist/denoise.d.ts +10 -1
- package/dist/denoise.d.ts.map +1 -1
- package/dist/denoise.js +4 -0
- package/dist/denoise.js.map +1 -1
- package/dist/device.d.ts.map +1 -1
- package/dist/device.js +8 -0
- package/dist/device.js.map +1 -1
- package/dist/frameLayout.d.ts.map +1 -1
- package/dist/frameLayout.js +6 -1
- package/dist/frameLayout.js.map +1 -1
- package/dist/imageResolve.d.ts +2 -1
- package/dist/imageResolve.d.ts.map +1 -1
- package/dist/imageResolve.js +1 -0
- package/dist/imageResolve.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lensPass.d.ts +4 -0
- package/dist/lensPass.d.ts.map +1 -1
- package/dist/lensPass.js +14 -1
- package/dist/lensPass.js.map +1 -1
- package/dist/lightTree.d.ts +10 -3
- package/dist/lightTree.d.ts.map +1 -1
- package/dist/lightTree.js +56 -17
- package/dist/lightTree.js.map +1 -1
- package/dist/lights.d.ts +20 -0
- package/dist/lights.d.ts.map +1 -1
- package/dist/lights.js +48 -1
- package/dist/lights.js.map +1 -1
- package/dist/oidnPass.d.ts +48 -0
- package/dist/oidnPass.d.ts.map +1 -0
- package/dist/oidnPass.js +255 -0
- package/dist/oidnPass.js.map +1 -0
- package/dist/pathTracePass.d.ts +2 -2
- package/dist/pathTracePass.d.ts.map +1 -1
- package/dist/pathTracePass.js +9 -9
- package/dist/pathTracePass.js.map +1 -1
- package/dist/presentPass.d.ts +2 -0
- package/dist/presentPass.d.ts.map +1 -1
- package/dist/presentPass.js +1 -0
- package/dist/presentPass.js.map +1 -1
- package/dist/renderer.d.ts +20 -0
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +138 -19
- package/dist/renderer.js.map +1 -1
- package/dist/sceneUpload.d.ts +5 -0
- package/dist/sceneUpload.d.ts.map +1 -1
- package/dist/sceneUpload.js +4 -0
- package/dist/sceneUpload.js.map +1 -1
- package/dist/settings.d.ts +29 -1
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +15 -6
- package/dist/settings.js.map +1 -1
- package/dist/wgsl/lens.wgsl.d.ts +1 -1
- package/dist/wgsl/lens.wgsl.d.ts.map +1 -1
- package/dist/wgsl/lens.wgsl.js +110 -17
- package/dist/wgsl/lens.wgsl.js.map +1 -1
- package/dist/wgsl/lights.wgsl.d.ts +1 -1
- package/dist/wgsl/lights.wgsl.d.ts.map +1 -1
- package/dist/wgsl/lights.wgsl.js +197 -33
- package/dist/wgsl/lights.wgsl.js.map +1 -1
- package/dist/wgsl/pathTrace.wgsl.d.ts +1 -1
- package/dist/wgsl/pathTrace.wgsl.d.ts.map +1 -1
- package/dist/wgsl/pathTrace.wgsl.js +173 -131
- package/dist/wgsl/pathTrace.wgsl.js.map +1 -1
- package/dist/wgsl/present.wgsl.d.ts +1 -1
- package/dist/wgsl/present.wgsl.d.ts.map +1 -1
- package/dist/wgsl/present.wgsl.js +11 -1
- package/dist/wgsl/present.wgsl.js.map +1 -1
- package/dist/wgsl/random.wgsl.d.ts +1 -1
- package/dist/wgsl/random.wgsl.d.ts.map +1 -1
- package/dist/wgsl/random.wgsl.js +28 -0
- package/dist/wgsl/random.wgsl.js.map +1 -1
- package/package.json +4 -2
- package/src/denoise.ts +15 -1
- package/src/device.ts +9 -0
- package/src/frameLayout.ts +6 -1
- package/src/imageResolve.ts +2 -0
- package/src/index.ts +2 -1
- package/src/lensPass.ts +17 -1
- package/src/lightTree.ts +67 -21
- package/src/lights.ts +65 -1
- package/src/oidnPass.ts +284 -0
- package/src/oidnWebgpu.d.ts +5 -0
- package/src/pathTracePass.ts +11 -9
- package/src/presentPass.ts +3 -0
- package/src/renderer.ts +161 -20
- package/src/sceneUpload.ts +9 -0
- package/src/settings.ts +40 -6
- package/src/wgsl/lens.wgsl.ts +110 -17
- package/src/wgsl/lights.wgsl.ts +197 -33
- package/src/wgsl/pathTrace.wgsl.ts +173 -131
- package/src/wgsl/present.wgsl.ts +11 -1
- package/src/wgsl/random.wgsl.ts +28 -0
- package/weights/LICENSE.txt +202 -0
- package/weights/rt_hdr_alb_nrm.tza +0 -0
package/dist/settings.d.ts
CHANGED
|
@@ -19,6 +19,12 @@ export interface GpuRenderSettings extends Omit<RenderSettings, "globalIlluminat
|
|
|
19
19
|
* gets the samples a large panel far away would have taken.
|
|
20
20
|
*/
|
|
21
21
|
lightTree: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Inside a room, aims at the sky through its windows (every thin transmissive material) as well as by its
|
|
24
|
+
* brightness, as Cycles' portals do, so daylight that enters through glass is found instead of stumbled upon.
|
|
25
|
+
* A point counts as inside when something is above it; elsewhere the sky is sampled as before.
|
|
26
|
+
*/
|
|
27
|
+
portals: boolean;
|
|
22
28
|
};
|
|
23
29
|
/**
|
|
24
30
|
* The light a real lens and sensor scatter around anything bright: a soft halo that fades with distance. Energy
|
|
@@ -59,11 +65,24 @@ export interface GpuRenderSettings extends Omit<RenderSettings, "globalIlluminat
|
|
|
59
65
|
};
|
|
60
66
|
/** A camera's exposure triangle and white balance. */
|
|
61
67
|
camera: {
|
|
68
|
+
/**
|
|
69
|
+
* The contrast and saturation of AgX's look, `0` to `1`: `0` is its base curve, flat on purpose, and `1` a look
|
|
70
|
+
* like Blender's Punchy, pivoted on middle grey so the image keeps its brightness. The other tone mappings ignore it.
|
|
71
|
+
*/
|
|
72
|
+
contrast: number;
|
|
62
73
|
/**
|
|
63
74
|
* Exposes from `iso`, `shutterSpeed` and `depthOfField.aperture` as a camera would, with `exposure` as a
|
|
64
75
|
* compensation on top. The scene's lights then have to be in physical units (lux, candela, nits).
|
|
65
76
|
*/
|
|
66
77
|
physicalExposure: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Meters the image as a camera in automatic does and exposes it to middle grey, from the mean of the logarithms of
|
|
80
|
+
* its luminance, weighed towards the centre: a bright window or sky cannot darken the rest. It takes the place of
|
|
81
|
+
* `exposure` and of the physical exposure, with `exposureCompensation` on top.
|
|
82
|
+
*/
|
|
83
|
+
autoExposure: boolean;
|
|
84
|
+
/** Stops added to the metered exposure: positive brightens, as on a camera's dial. */
|
|
85
|
+
exposureCompensation: number;
|
|
67
86
|
iso: number;
|
|
68
87
|
/** Seconds. */
|
|
69
88
|
shutterSpeed: number;
|
|
@@ -77,6 +96,12 @@ export interface GpuRenderSettings extends Omit<RenderSettings, "globalIlluminat
|
|
|
77
96
|
subsurface: {
|
|
78
97
|
/** Materials with `userData.cpuRenderer.subsurface` scatter light inside themselves. Off renders them opaque. */
|
|
79
98
|
enabled: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Bounces at which light still walks inside such a material: past them it reflects off it as a diffuse surface of
|
|
101
|
+
* the same colour. Light that has already bounced that often hardly shows the difference, and every walk costs as
|
|
102
|
+
* much as many bounces.
|
|
103
|
+
*/
|
|
104
|
+
depth: number;
|
|
80
105
|
};
|
|
81
106
|
/**
|
|
82
107
|
* Aerial perspective: the air between the camera and what it sees, which fades distant things towards the colour of
|
|
@@ -121,7 +146,10 @@ export declare function mergeGpuSettings(base: GpuRenderSettings, input?: GpuRen
|
|
|
121
146
|
* sensor, from the exposure value at ISO 100 (Lagarde and de Rousiers, "Moving Frostbite to PBR", 2014).
|
|
122
147
|
*/
|
|
123
148
|
export declare function physicalExposure(aperture: number, shutterSpeed: number, iso: number): number;
|
|
124
|
-
/**
|
|
149
|
+
/**
|
|
150
|
+
* The exposure the image is multiplied by: the setting itself, or a camera's on top of it as compensation, or with
|
|
151
|
+
* the automatic exposure only its compensation, the meter's own exposure being applied on the device.
|
|
152
|
+
*/
|
|
125
153
|
export declare function effectiveExposure(settings: GpuRenderSettings): number;
|
|
126
154
|
type Matrix = number[];
|
|
127
155
|
/** The temperature the image is neutral at: white balance at this value changes nothing. */
|
package/dist/settings.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"settings.d.ts","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0C,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AAE5F;;;;GAIG;AACH,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,cAAc,EAAE,oBAAoB,CAAC;IACnF,kBAAkB,EAAE,cAAc,CAAC,oBAAoB,CAAC,GAAG;QACzD;;;WAGG;QACH,eAAe,EAAE,OAAO,CAAC;QACzB,uGAAuG;QACvG,aAAa,EAAE,MAAM,CAAC;QACtB;;;;WAIG;QACH,SAAS,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"settings.d.ts","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"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"}
|
package/dist/settings.js
CHANGED
|
@@ -8,13 +8,16 @@ export const DEFAULT_GPU_RENDER_SETTINGS = Object.freeze(mergeGpuSettings({
|
|
|
8
8
|
...DEFAULT_RENDER_SETTINGS,
|
|
9
9
|
// AgX rolls highlights off as film does, where ACES pushes saturated colours to the gamut's edge.
|
|
10
10
|
toneMapping: "agx",
|
|
11
|
-
globalIllumination: { ...DEFAULT_RENDER_SETTINGS.globalIllumination, russianRoulette: true, rouletteStart:
|
|
12
|
-
lightTree: true },
|
|
11
|
+
globalIllumination: { ...DEFAULT_RENDER_SETTINGS.globalIllumination, russianRoulette: true, rouletteStart: 1,
|
|
12
|
+
lightTree: true, portals: true },
|
|
13
13
|
bloom: { enabled: true, intensity: 0.04, radius: 0.65 },
|
|
14
14
|
glare: { enabled: true, intensity: 0.06, blades: 6, length: 0.25, rotation: 15 },
|
|
15
|
-
lens: { chromaticAberration: 0.
|
|
16
|
-
camera: {
|
|
17
|
-
|
|
15
|
+
lens: { chromaticAberration: 0.05, distortion: 0 },
|
|
16
|
+
camera: {
|
|
17
|
+
contrast: 1, physicalExposure: false, autoExposure: false, exposureCompensation: 0, iso: 100,
|
|
18
|
+
shutterSpeed: 1 / 125, whiteBalance: 6500,
|
|
19
|
+
},
|
|
20
|
+
subsurface: { enabled: true, depth: 2 },
|
|
18
21
|
atmosphere: { enabled: true, visibility: 20000, haze: 0.6, color: "" },
|
|
19
22
|
volumes: { enabled: true },
|
|
20
23
|
bevel: { enabled: true },
|
|
@@ -35,9 +38,15 @@ export function physicalExposure(aperture, shutterSpeed, iso) {
|
|
|
35
38
|
const ev100 = Math.log2(n * n / t * 100 / s);
|
|
36
39
|
return 1 / (1.2 * 2 ** ev100);
|
|
37
40
|
}
|
|
38
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* The exposure the image is multiplied by: the setting itself, or a camera's on top of it as compensation, or with
|
|
43
|
+
* the automatic exposure only its compensation, the meter's own exposure being applied on the device.
|
|
44
|
+
*/
|
|
39
45
|
export function effectiveExposure(settings) {
|
|
40
46
|
const { camera, depthOfField, exposure } = settings;
|
|
47
|
+
// The meter's exposure is applied on the device, by the lens pass; what is left here is the compensation.
|
|
48
|
+
if (camera.autoExposure)
|
|
49
|
+
return 2 ** (Number(camera.exposureCompensation) || 0);
|
|
41
50
|
return camera.physicalExposure
|
|
42
51
|
? exposure * physicalExposure(depthOfField.aperture, camera.shutterSpeed, camera.iso)
|
|
43
52
|
: exposure;
|
package/dist/settings.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"settings.js","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAuB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"settings.js","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAuB,MAAM,eAAe,CAAC;AA4I5F;;;;GAIG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAgC,MAAM,CAAC,MAAM,CAAC,gBAAgB,CACpG;IACE,GAAG,uBAAuB;IAC1B,kGAAkG;IAClG,WAAW,EAAE,KAAK;IAClB,kBAAkB,EAAE,EAAE,GAAG,uBAAuB,CAAC,kBAAkB,EAAE,eAAe,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;QAC1G,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;IAClC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;IACvD,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE;IAChF,IAAI,EAAE,EAAE,mBAAmB,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE;IAClD,MAAM,EAAE;QACN,QAAQ,EAAE,CAAC,EAAE,gBAAgB,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,oBAAoB,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG;QAC5F,YAAY,EAAE,CAAC,GAAG,GAAG,EAAE,YAAY,EAAE,IAAI;KAC1C;IACD,UAAU,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE;IACvC,UAAU,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE;IACtE,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;IAC1B,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;CACzB;AACD,iHAAiH;AACjH,+EAA+E;AAC/E,EAAE,kBAAkB,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;AAEvD,6GAA6G;AAC7G,MAAM,UAAU,gBAAgB,CAAC,IAAuB,EAAE,QAAgC,EAAE;IAC1F,OAAO,aAAa,CAAC,IAAiC,EAAE,KAAc,CAAiC,CAAC;AAC1G,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAgB,EAAE,YAAoB,EAAE,GAAW;IAClF,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IAC1F,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC;IAE7C,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,IAAI,KAAK,CAAC,CAAC;AAChC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAA2B;IAC3D,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC;IAEpD,0GAA0G;IAC1G,IAAI,MAAM,CAAC,YAAY;QAAE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC;IAEhF,OAAO,MAAM,CAAC,gBAAgB;QAC5B,CAAC,CAAC,QAAQ,GAAG,gBAAgB,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,GAAG,CAAC;QACrF,CAAC,CAAC,QAAQ,CAAC;AACf,CAAC;AAED,qHAAqH;AACrH,SAAS,SAAS,CAAC,MAAc;IAC/B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAElD,MAAM,CAAC,GAAG,CAAC,IAAI,IAAI;QACjB,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC,IAAI,CAAC,GAAG,WAAW,GAAG,CAAC,IAAI,CAAC,GAAG,WAAW,GAAG,CAAC,GAAG,OAAO;QAC1E,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC,IAAI,CAAC,GAAG,WAAW,GAAG,CAAC,IAAI,CAAC,GAAG,WAAW,GAAG,CAAC,GAAG,OAAO,CAAC;IAE7E,MAAM,CAAC,GAAG,CAAC,IAAI,IAAI;QACjB,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,GAAG,SAAS,GAAG,CAAC,IAAI,CAAC,GAAG,UAAU,GAAG,CAAC,GAAG,UAAU;QACxE,CAAC,CAAC,CAAC,IAAI,IAAI;YACT,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,GAAG,UAAU,GAAG,CAAC,IAAI,CAAC,GAAG,UAAU,GAAG,CAAC,GAAG,UAAU;YACzE,CAAC,CAAC,QAAQ,GAAG,CAAC,IAAI,CAAC,GAAG,SAAS,GAAG,CAAC,IAAI,CAAC,GAAG,UAAU,GAAG,CAAC,GAAG,UAAU,CAAC;IAE3E,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAChB,CAAC;AAID,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;IACpF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC;IAE9C,OAAO,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;AAClG,CAAC,CAAC,CAAC;AAEH,MAAM,KAAK,GAAG,CAAC,CAAS,EAAE,CAAW,EAAE,EAAE,CACvC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAE1F,MAAM,WAAW,GAAG;IAClB,SAAS,EAAE,SAAS,EAAE,SAAS;IAC/B,SAAS,EAAE,SAAS,EAAE,QAAQ;IAC9B,SAAS,EAAE,QAAQ,EAAE,SAAS;CAAC,CAAC;AAElC,MAAM,WAAW,GAAG;IAClB,SAAS,EAAE,CAAC,SAAS,EAAE,CAAC,SAAS;IACjC,CAAC,QAAQ,EAAE,SAAS,EAAE,QAAQ;IAC9B,SAAS,EAAE,CAAC,SAAS,EAAE,SAAS;CAAC,CAAC;AAEpC,MAAM,QAAQ,GAAG;IACf,MAAM,EAAE,MAAM,EAAE,CAAC,MAAM;IACvB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM;IACvB,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM;CAAC,CAAC;AAE3B,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,CAAC,SAAS,EAAE,SAAS;IAChC,SAAS,EAAE,SAAS,EAAE,SAAS;IAC/B,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS;CAAC,CAAC;AAEpC,4DAA4D;AAC5D,SAAS,SAAS,CAAC,MAAc;IAC/B,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IAEjC,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC;AAED,4FAA4F;AAC5F,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAE1C;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAc;IAC/C,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,qBAAqB,CAAC,CAAC;IAC5E,MAAM,KAAK,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAEtG,OAAO,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7G,CAAC"}
|
package/dist/wgsl/lens.wgsl.d.ts
CHANGED
|
@@ -13,5 +13,5 @@
|
|
|
13
13
|
* texel takes the light of the texels behind it along every ray's direction, fading with the distance, with weights
|
|
14
14
|
* that again sum to one.
|
|
15
15
|
*/
|
|
16
|
-
export declare const LENS_WGSL = "\nstruct Lens {\n resolution: vec2<u32>,\n // Whether the colour comes from the denoiser's output rather than straight from the accumulation.\n denoised: u32,\n // Upsampling only: the level read from sits in the downsampled pyramid (the smallest one) instead of the folded one.\n fromDown: u32,\n sourceSize: vec2<u32>,\n // Where the level read from starts in its pyramid; a size of the full resolution reads the image itself.\n sourceOffset: u32,\n // The first downsample reads the image itself, and averages its taps by luminance against fireflies.\n fromImage: u32,\n targetSize: vec2<u32>,\n targetOffset: u32,\n // Where the largest level of the folded pyramid starts, which the composite reads the halo from.\n haloOffset: u32,\n haloSize: vec2<u32>,\n // How much of the light the halo takes, and how much each level takes from the one below it.\n intensity: f32,\n radius: f32,\n // How far red and blue move apart at the corners, as a share of the distance from the centre.\n aberration: f32,\n // How much of the light the aperture's rays take, how many there are, and how far they reach in streak texels.\n glare: f32,\n rays: u32,\n glareLength: f32,\n // The white balance, a linear sRGB matrix, row by row.\n white0: vec4<f32>,\n white1: vec4<f32>,\n white2: vec4<f32>,\n // The size of the level the rays are gathered at, and the angle of the first ray, in radians.\n streakSize: vec2<u32>,\n glareAngle: f32,\n pad0: f32,\n};\n\nconst PI: f32 = 3.14159265358979;\n\n/** Taps along each of the aperture's rays. */\nconst STREAK_TAPS: i32 = 24;\n\n@group(0) @binding(0) var<uniform> lens: Lens;\n@group(0) @binding(1) var<storage, read> accumulation: array<vec4<f32>>;\n@group(0) @binding(2) var<storage, read> denoised: array<vec4<f32>>;\n@group(0) @binding(3) var<storage, read_write> down: array<vec4<f32>>;\n@group(0) @binding(4) var<storage, read_write> up: array<vec4<f32>>;\n@group(0) @binding(5) var<storage, read_write> graded: array<vec4<f32>>;\n@group(0) @binding(6) var<storage, read_write> streaked: array<vec4<f32>>;\n\n/** The image's own texel: its mean so far, or nothing where no path has landed yet. */\nfn imageTexel(x: i32, y: i32) -> vec3<f32> {\n let size = vec2<i32>(lens.resolution);\n let index = u32(clamp(y, 0, size.y - 1) * size.x + clamp(x, 0, size.x - 1));\n\n if (lens.denoised != 0u) {\n return denoised[index].rgb;\n }\n\n let stored = accumulation[index];\n\n return select(stored.rgb / stored.w, vec3<f32>(0.0), stored.w <= 0.0);\n}\n\nfn downTexel(offset: u32, size: vec2<u32>, x: i32, y: i32) -> vec3<f32> {\n let s = vec2<i32>(size);\n\n return down[offset + u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;\n}\n\nfn upTexel(offset: u32, size: vec2<u32>, x: i32, y: i32) -> vec3<f32> {\n let s = vec2<i32>(size);\n\n return up[offset + u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;\n}\n\n/** Bilinear weights and the texel below and left of a point given in texels, edges clamped. */\nfn bilinearAt(position: vec2<f32>) -> vec4<f32> {\n let p = position - 0.5;\n let base = floor(p);\n\n return vec4<f32>(base, p - base);\n}\n\nfn imageBilinear(position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(\n mix(imageTexel(x, y), imageTexel(x + 1, y), b.z),\n mix(imageTexel(x, y + 1), imageTexel(x + 1, y + 1), b.z), b.w);\n}\n\nfn downBilinear(offset: u32, size: vec2<u32>, position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(\n mix(downTexel(offset, size, x, y), downTexel(offset, size, x + 1, y), b.z),\n mix(downTexel(offset, size, x, y + 1), downTexel(offset, size, x + 1, y + 1), b.z), b.w);\n}\n\nfn upBilinear(offset: u32, size: vec2<u32>, position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(\n mix(upTexel(offset, size, x, y), upTexel(offset, size, x + 1, y), b.z),\n mix(upTexel(offset, size, x, y + 1), upTexel(offset, size, x + 1, y + 1), b.z), b.w);\n}\n\n/** The level being filtered, read bilinearly at a point given in its own texels. */\nfn sourceAt(position: vec2<f32>) -> vec3<f32> {\n if (lens.fromImage != 0u) {\n return imageBilinear(position);\n }\n\n return downBilinear(lens.sourceOffset, lens.sourceSize, position);\n}\n\nfn luminance(color: vec3<f32>) -> f32 {\n return dot(color, vec3<f32>(0.2126, 0.7152, 0.0722));\n}\n\n/** Karis' average: a group of taps weighed by one over one plus its luminance, so a lone firefly cannot flare. */\nfn karis(sum: vec3<f32>) -> vec4<f32> {\n let weight = 1.0 / (1.0 + luminance(sum));\n\n return vec4<f32>(sum * weight, weight);\n}\n\n/** One level of the pyramid from the one above it: Jimenez's 13 taps, four overlapping boxes and a centre one. */\n@compute @workgroup_size(8, 8, 1)\nfn downsample(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {\n return;\n }\n\n let scale = vec2<f32>(lens.sourceSize) / vec2<f32>(lens.targetSize);\n let centre = (vec2<f32>(id.xy) + 0.5) * scale;\n let a = sourceAt(centre + vec2<f32>(-2.0, -2.0));\n let b = sourceAt(centre + vec2<f32>(0.0, -2.0));\n let c = sourceAt(centre + vec2<f32>(2.0, -2.0));\n let d = sourceAt(centre + vec2<f32>(-2.0, 0.0));\n let e = sourceAt(centre);\n let f = sourceAt(centre + vec2<f32>(2.0, 0.0));\n let g = sourceAt(centre + vec2<f32>(-2.0, 2.0));\n let h = sourceAt(centre + vec2<f32>(0.0, 2.0));\n let i = sourceAt(centre + vec2<f32>(2.0, 2.0));\n let j = sourceAt(centre + vec2<f32>(-1.0, -1.0));\n let k = sourceAt(centre + vec2<f32>(1.0, -1.0));\n let l = sourceAt(centre + vec2<f32>(-1.0, 1.0));\n let m = sourceAt(centre + vec2<f32>(1.0, 1.0));\n var result: vec3<f32>;\n\n if (lens.fromImage != 0u) {\n let groups = karis((j + k + l + m) * 0.25) * 0.5\n + karis((a + b + d + e) * 0.25) * 0.125\n + karis((b + c + e + f) * 0.25) * 0.125\n + karis((d + e + g + h) * 0.25) * 0.125\n + karis((e + f + h + i) * 0.25) * 0.125;\n\n result = groups.rgb / max(groups.w, 1e-6);\n } else {\n result = e * 0.125 + (a + c + g + i) * 0.03125 + (b + d + f + h) * 0.0625 + (j + k + l + m) * 0.125;\n }\n\n down[lens.targetOffset + id.y * lens.targetSize.x + id.x] = vec4<f32>(max(result, vec3<f32>(0.0)), 1.0);\n}\n\n/** A 3 x 3 tent over the smaller level, read at a point given in its texels. */\nfn tent(position: vec2<f32>) -> vec3<f32> {\n var sum = vec3<f32>(0.0);\n\n for (var dy = -1; dy <= 1; dy = dy + 1) {\n for (var dx = -1; dx <= 1; dx = dx + 1) {\n let weight = f32((2 - abs(dx)) * (2 - abs(dy))) / 16.0;\n let at = position + vec2<f32>(f32(dx), f32(dy));\n\n if (lens.fromDown != 0u) {\n sum = sum + downBilinear(lens.sourceOffset, lens.sourceSize, at) * weight;\n } else {\n sum = sum + upBilinear(lens.sourceOffset, lens.sourceSize, at) * weight;\n }\n }\n }\n\n return sum;\n}\n\n/** One level folded back: its own light, mixed by the radius with the wider spread of every level below it. */\n@compute @workgroup_size(8, 8, 1)\nfn upsample(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {\n return;\n }\n\n let scale = vec2<f32>(lens.sourceSize) / vec2<f32>(lens.targetSize);\n let wider = tent((vec2<f32>(id.xy) + 0.5) * scale);\n let index = lens.targetOffset + id.y * lens.targetSize.x + id.x;\n\n up[index] = vec4<f32>(mix(down[index].rgb, wider, lens.radius), 1.0);\n}\n\n/** The aperture's rays at one texel of the streak level: the light behind it along each ray, fading with distance. */\n@compute @workgroup_size(8, 8, 1)\nfn streaks(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {\n return;\n }\n\n let centre = vec2<f32>(id.xy) + 0.5;\n var sum = vec3<f32>(0.0);\n var total = 0.0;\n\n for (var r = 0u; r < lens.rays; r = r + 1u) {\n let angle = lens.glareAngle + f32(r) * 2.0 * PI / f32(lens.rays);\n let along = vec2<f32>(cos(angle), sin(angle));\n\n for (var j = 1; j <= STREAK_TAPS; j = j + 1) {\n let t = f32(j) / f32(STREAK_TAPS);\n let weight = exp(-4.0 * t);\n\n sum = sum + downBilinear(lens.sourceOffset, lens.sourceSize, centre - along * (t * lens.glareLength)) * weight;\n total = total + weight;\n }\n }\n\n streaked[id.y * lens.targetSize.x + id.x] = vec4<f32>(sum / max(total, 1e-6), 1.0);\n}\n\nfn streakTexel(x: i32, y: i32) -> vec3<f32> {\n let s = vec2<i32>(lens.streakSize);\n\n return streaked[u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;\n}\n\nfn streakBilinear(position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(mix(streakTexel(x, y), streakTexel(x + 1, y), b.z), mix(streakTexel(x, y + 1), streakTexel(x + 1, y + 1), b.z), b.w);\n}\n\n/** The folded halo at full resolution: a tent over its largest level, which is half the image's size. */\nfn tentHalo(position: vec2<f32>) -> vec3<f32> {\n var sum = vec3<f32>(0.0);\n\n for (var dy = -1; dy <= 1; dy = dy + 1) {\n for (var dx = -1; dx <= 1; dx = dx + 1) {\n let weight = f32((2 - abs(dx)) * (2 - abs(dy))) / 16.0;\n\n sum = sum + upBilinear(lens.haloOffset, lens.haloSize, position + vec2<f32>(f32(dx), f32(dy)) * 0.5) * weight;\n }\n }\n\n return sum;\n}\n\n/** The image as the lens delivers it: fringed towards the corners, with its halo, balanced for the camera's white. */\n@compute @workgroup_size(8, 8, 1)\nfn composite(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.resolution.x || id.y >= lens.resolution.y) {\n return;\n }\n\n let size = vec2<f32>(lens.resolution);\n let position = vec2<f32>(id.xy) + 0.5;\n var color = imageTexel(i32(id.x), i32(id.y));\n\n // Lateral chromatic aberration: each colour is magnified a little differently, red outwards and blue inwards,\n // in proportion to the distance from the centre, which is how a lens's dispersion moves them.\n if (lens.aberration > 0.0) {\n let centre = size * 0.5;\n let away = position - centre;\n let red = imageBilinear(centre + away * (1.0 - lens.aberration));\n let blue = imageBilinear(centre + away * (1.0 + lens.aberration));\n\n color = vec3<f32>(red.r, color.g, blue.b);\n }\n\n if (lens.intensity > 0.0) {\n let halo = tentHalo(position * vec2<f32>(lens.haloSize) / size);\n\n color = mix(color, halo, lens.intensity);\n }\n\n if (lens.glare > 0.0) {\n color = mix(color, streakBilinear(position * vec2<f32>(lens.streakSize) / size), lens.glare);\n }\n\n let balanced = vec3<f32>(dot(lens.white0.xyz, color), dot(lens.white1.xyz, color), dot(lens.white2.xyz, color));\n\n graded[id.y * lens.resolution.x + id.x] = vec4<f32>(max(balanced, vec3<f32>(0.0)), 1.0);\n}\n";
|
|
16
|
+
export declare const LENS_WGSL = "\nstruct Lens {\n resolution: vec2<u32>,\n // Whether the colour comes from the denoiser's output rather than straight from the accumulation.\n denoised: u32,\n // Upsampling only: the level read from sits in the downsampled pyramid (the smallest one) instead of the folded one.\n fromDown: u32,\n sourceSize: vec2<u32>,\n // Where the level read from starts in its pyramid; a size of the full resolution reads the image itself.\n sourceOffset: u32,\n // The first downsample reads the image itself, and averages its taps by luminance against fireflies.\n fromImage: u32,\n targetSize: vec2<u32>,\n targetOffset: u32,\n // Where the largest level of the folded pyramid starts, which the composite reads the halo from.\n haloOffset: u32,\n haloSize: vec2<u32>,\n // How much of the light the halo takes, and how much each level takes from the one below it.\n intensity: f32,\n radius: f32,\n // How far red and blue move apart at the corners, as a share of the distance from the centre.\n aberration: f32,\n // How much of the light the aperture's rays take, how many there are, and how far they reach in streak texels.\n glare: f32,\n rays: u32,\n glareLength: f32,\n // The white balance, a linear sRGB matrix, row by row.\n white0: vec4<f32>,\n white1: vec4<f32>,\n white2: vec4<f32>,\n // The size of the level the rays are gathered at, and the angle of the first ray, in radians.\n streakSize: vec2<u32>,\n glareAngle: f32,\n // Whether the camera meters the image and exposes it to middle grey, which the meter pass works out.\n autoExposure: f32,\n};\n\nconst PI: f32 = 3.14159265358979;\n\n/** Taps along each of the aperture's rays. */\nconst STREAK_TAPS: i32 = 24;\n\n@group(0) @binding(0) var<uniform> lens: Lens;\n@group(0) @binding(1) var<storage, read> accumulation: array<vec4<f32>>;\n@group(0) @binding(2) var<storage, read> denoised: array<vec4<f32>>;\n@group(0) @binding(3) var<storage, read_write> down: array<vec4<f32>>;\n@group(0) @binding(4) var<storage, read_write> up: array<vec4<f32>>;\n@group(0) @binding(5) var<storage, read_write> graded: array<vec4<f32>>;\n@group(0) @binding(6) var<storage, read_write> streaked: array<vec4<f32>>;\n// What the camera's meter read: the exposure that brings the image's average to middle grey, in x.\n@group(0) @binding(7) var<storage, read_write> metered: array<vec4<f32>>;\n\n/** What a light meter calibrates to: an average scene reflects about eighteen percent. */\nconst MIDDLE_GREY: f32 = 0.18;\nconst METER_GRID: vec2<u32> = vec2<u32>(64u, 36u);\nconst METER_THREADS: u32 = 256u;\n\n/**\n * How far above middle grey the meter lets the bright end of the image land: three stops, where AgX still shows\n * detail, three and a half under where it reaches white.\n */\nconst HIGHLIGHT_CEILING: f32 = 8.0;\n/** The share of the metered points that counts as the bright end: the light through a window, not a lamp's own pixels. */\nconst HIGHLIGHT_SHARE: f32 = 0.03;\n/** Half stops of luminance, from 2^-16 to 2^16, which the bright end is found in. */\nconst METER_BINS: u32 = 64u;\n\nvar<workgroup> meterSums: array<vec2<f32>, 256>;\nvar<workgroup> meterHistogram: array<atomic<u32>, 64>;\n\n/**\n * The camera's meter: the image's average luminance over a grid of points, taken as the mean of the logarithms so that\n * a bright window or a patch of sky cannot darken everything else, weighed towards the centre as a camera's\n * centre-weighted metering is. What no path has reached yet, or is black, does not count. One workgroup reads the\n * grid and sums it in shared memory.\n *\n * Then it guards the highlights, as a camera's matrix metering does: the average alone takes a dim attic with a shaft\n * of sun in it to middle grey and burns the shaft out, where a photographer keeps the room dim. The brightest few\n * percent of the points, found in a histogram of half stops, may land no more than {@link HIGHLIGHT_CEILING} times\n * brighter; an even scene, whose bright end is close to its average, is left as the average says.\n */\n@compute @workgroup_size(256, 1, 1)\nfn meter(@builtin(local_invocation_index) thread: u32) {\n var sum = vec2<f32>(0.0);\n let count = METER_GRID.x * METER_GRID.y;\n\n if (thread < METER_BINS) {\n atomicStore(&meterHistogram[thread], 0u);\n }\n\n workgroupBarrier();\n\n for (var k = thread; k < count; k = k + METER_THREADS) {\n let cell = vec2<f32>(f32(k % METER_GRID.x), f32(k / METER_GRID.x)) + 0.5;\n let uv = cell / vec2<f32>(METER_GRID);\n let luminance = dot(imageTexel(i32(uv.x * f32(lens.resolution.x)), i32(uv.y * f32(lens.resolution.y))),\n vec3<f32>(0.2126, 0.7152, 0.0722));\n let away = (uv - 0.5) * 2.0;\n let weight = select(0.0, 1.0 - 0.6 * min(dot(away, away) * 0.5, 1.0), luminance > 0.0);\n\n sum = sum + vec2<f32>(log(max(luminance, 1e-6)) * weight, weight);\n\n if (luminance > 0.0) {\n let bin = u32(clamp((log2(luminance) + 16.0) * 2.0, 0.0, f32(METER_BINS - 1u)));\n\n atomicAdd(&meterHistogram[bin], 1u);\n }\n }\n\n meterSums[thread] = sum;\n workgroupBarrier();\n\n for (var stride = METER_THREADS / 2u; stride > 0u; stride = stride / 2u) {\n if (thread < stride) {\n meterSums[thread] = meterSums[thread] + meterSums[thread + stride];\n }\n\n workgroupBarrier();\n }\n\n if (thread == 0u) {\n let total = meterSums[0];\n let average = select(MIDDLE_GREY, exp(total.x / max(total.y, 1e-6)), total.y > 0.0);\n var exposure = MIDDLE_GREY / average;\n\n // From the top down, the first half stop by which the bright share is reached; its upper edge is that end.\n var points = 0u;\n\n for (var bin = 0u; bin < METER_BINS; bin = bin + 1u) {\n points = points + atomicLoad(&meterHistogram[bin]);\n }\n\n var above = 0u;\n var bright = 0.0;\n\n for (var bin = i32(METER_BINS) - 1; bin >= 0 && points > 0u; bin = bin - 1) {\n above = above + atomicLoad(&meterHistogram[u32(bin)]);\n\n if (f32(above) >= HIGHLIGHT_SHARE * f32(points)) {\n bright = exp2(f32(bin + 1) * 0.5 - 16.0);\n break;\n }\n }\n\n if (bright > 0.0) {\n exposure = min(exposure, HIGHLIGHT_CEILING * MIDDLE_GREY / bright);\n }\n\n metered[0] = vec4<f32>(clamp(exposure, 1e-4, 1e4), average, bright, 0.0);\n }\n}\n\n/** The image's own texel: its mean so far, or nothing where no path has landed yet. */\nfn imageTexel(x: i32, y: i32) -> vec3<f32> {\n let size = vec2<i32>(lens.resolution);\n let index = u32(clamp(y, 0, size.y - 1) * size.x + clamp(x, 0, size.x - 1));\n\n if (lens.denoised != 0u) {\n return denoised[index].rgb;\n }\n\n let stored = accumulation[index];\n\n return select(stored.rgb / stored.w, vec3<f32>(0.0), stored.w <= 0.0);\n}\n\nfn downTexel(offset: u32, size: vec2<u32>, x: i32, y: i32) -> vec3<f32> {\n let s = vec2<i32>(size);\n\n return down[offset + u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;\n}\n\nfn upTexel(offset: u32, size: vec2<u32>, x: i32, y: i32) -> vec3<f32> {\n let s = vec2<i32>(size);\n\n return up[offset + u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;\n}\n\n/** Bilinear weights and the texel below and left of a point given in texels, edges clamped. */\nfn bilinearAt(position: vec2<f32>) -> vec4<f32> {\n let p = position - 0.5;\n let base = floor(p);\n\n return vec4<f32>(base, p - base);\n}\n\nfn imageBilinear(position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(\n mix(imageTexel(x, y), imageTexel(x + 1, y), b.z),\n mix(imageTexel(x, y + 1), imageTexel(x + 1, y + 1), b.z), b.w);\n}\n\nfn downBilinear(offset: u32, size: vec2<u32>, position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(\n mix(downTexel(offset, size, x, y), downTexel(offset, size, x + 1, y), b.z),\n mix(downTexel(offset, size, x, y + 1), downTexel(offset, size, x + 1, y + 1), b.z), b.w);\n}\n\nfn upBilinear(offset: u32, size: vec2<u32>, position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(\n mix(upTexel(offset, size, x, y), upTexel(offset, size, x + 1, y), b.z),\n mix(upTexel(offset, size, x, y + 1), upTexel(offset, size, x + 1, y + 1), b.z), b.w);\n}\n\n/** The level being filtered, read bilinearly at a point given in its own texels. */\nfn sourceAt(position: vec2<f32>) -> vec3<f32> {\n if (lens.fromImage != 0u) {\n return imageBilinear(position);\n }\n\n return downBilinear(lens.sourceOffset, lens.sourceSize, position);\n}\n\nfn luminance(color: vec3<f32>) -> f32 {\n return dot(color, vec3<f32>(0.2126, 0.7152, 0.0722));\n}\n\n/** Karis' average: a group of taps weighed by one over one plus its luminance, so a lone firefly cannot flare. */\nfn karis(sum: vec3<f32>) -> vec4<f32> {\n let weight = 1.0 / (1.0 + luminance(sum));\n\n return vec4<f32>(sum * weight, weight);\n}\n\n/** One level of the pyramid from the one above it: Jimenez's 13 taps, four overlapping boxes and a centre one. */\n@compute @workgroup_size(8, 8, 1)\nfn downsample(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {\n return;\n }\n\n let scale = vec2<f32>(lens.sourceSize) / vec2<f32>(lens.targetSize);\n let centre = (vec2<f32>(id.xy) + 0.5) * scale;\n let a = sourceAt(centre + vec2<f32>(-2.0, -2.0));\n let b = sourceAt(centre + vec2<f32>(0.0, -2.0));\n let c = sourceAt(centre + vec2<f32>(2.0, -2.0));\n let d = sourceAt(centre + vec2<f32>(-2.0, 0.0));\n let e = sourceAt(centre);\n let f = sourceAt(centre + vec2<f32>(2.0, 0.0));\n let g = sourceAt(centre + vec2<f32>(-2.0, 2.0));\n let h = sourceAt(centre + vec2<f32>(0.0, 2.0));\n let i = sourceAt(centre + vec2<f32>(2.0, 2.0));\n let j = sourceAt(centre + vec2<f32>(-1.0, -1.0));\n let k = sourceAt(centre + vec2<f32>(1.0, -1.0));\n let l = sourceAt(centre + vec2<f32>(-1.0, 1.0));\n let m = sourceAt(centre + vec2<f32>(1.0, 1.0));\n var result: vec3<f32>;\n\n if (lens.fromImage != 0u) {\n let groups = karis((j + k + l + m) * 0.25) * 0.5\n + karis((a + b + d + e) * 0.25) * 0.125\n + karis((b + c + e + f) * 0.25) * 0.125\n + karis((d + e + g + h) * 0.25) * 0.125\n + karis((e + f + h + i) * 0.25) * 0.125;\n\n result = groups.rgb / max(groups.w, 1e-6);\n } else {\n result = e * 0.125 + (a + c + g + i) * 0.03125 + (b + d + f + h) * 0.0625 + (j + k + l + m) * 0.125;\n }\n\n down[lens.targetOffset + id.y * lens.targetSize.x + id.x] = vec4<f32>(max(result, vec3<f32>(0.0)), 1.0);\n}\n\n/** A 3 x 3 tent over the smaller level, read at a point given in its texels. */\nfn tent(position: vec2<f32>) -> vec3<f32> {\n var sum = vec3<f32>(0.0);\n\n for (var dy = -1; dy <= 1; dy = dy + 1) {\n for (var dx = -1; dx <= 1; dx = dx + 1) {\n let weight = f32((2 - abs(dx)) * (2 - abs(dy))) / 16.0;\n let at = position + vec2<f32>(f32(dx), f32(dy));\n\n if (lens.fromDown != 0u) {\n sum = sum + downBilinear(lens.sourceOffset, lens.sourceSize, at) * weight;\n } else {\n sum = sum + upBilinear(lens.sourceOffset, lens.sourceSize, at) * weight;\n }\n }\n }\n\n return sum;\n}\n\n/** One level folded back: its own light, mixed by the radius with the wider spread of every level below it. */\n@compute @workgroup_size(8, 8, 1)\nfn upsample(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {\n return;\n }\n\n let scale = vec2<f32>(lens.sourceSize) / vec2<f32>(lens.targetSize);\n let wider = tent((vec2<f32>(id.xy) + 0.5) * scale);\n let index = lens.targetOffset + id.y * lens.targetSize.x + id.x;\n\n up[index] = vec4<f32>(mix(down[index].rgb, wider, lens.radius), 1.0);\n}\n\n/** The aperture's rays at one texel of the streak level: the light behind it along each ray, fading with distance. */\n@compute @workgroup_size(8, 8, 1)\nfn streaks(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {\n return;\n }\n\n let centre = vec2<f32>(id.xy) + 0.5;\n var sum = vec3<f32>(0.0);\n var total = 0.0;\n\n for (var r = 0u; r < lens.rays; r = r + 1u) {\n let angle = lens.glareAngle + f32(r) * 2.0 * PI / f32(lens.rays);\n let along = vec2<f32>(cos(angle), sin(angle));\n\n for (var j = 1; j <= STREAK_TAPS; j = j + 1) {\n let t = f32(j) / f32(STREAK_TAPS);\n let weight = exp(-4.0 * t);\n\n sum = sum + downBilinear(lens.sourceOffset, lens.sourceSize, centre - along * (t * lens.glareLength)) * weight;\n total = total + weight;\n }\n }\n\n streaked[id.y * lens.targetSize.x + id.x] = vec4<f32>(sum / max(total, 1e-6), 1.0);\n}\n\nfn streakTexel(x: i32, y: i32) -> vec3<f32> {\n let s = vec2<i32>(lens.streakSize);\n\n return streaked[u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;\n}\n\nfn streakBilinear(position: vec2<f32>) -> vec3<f32> {\n let b = bilinearAt(position);\n let x = i32(b.x);\n let y = i32(b.y);\n\n return mix(mix(streakTexel(x, y), streakTexel(x + 1, y), b.z), mix(streakTexel(x, y + 1), streakTexel(x + 1, y + 1), b.z), b.w);\n}\n\n/** The image as the lens delivers it: fringed towards the corners, with its halo, balanced for the camera's white. */\n@compute @workgroup_size(8, 8, 1)\nfn composite(@builtin(global_invocation_id) id: vec3<u32>) {\n if (id.x >= lens.resolution.x || id.y >= lens.resolution.y) {\n return;\n }\n\n let size = vec2<f32>(lens.resolution);\n let position = vec2<f32>(id.xy) + 0.5;\n var color = imageTexel(i32(id.x), i32(id.y));\n\n // Lateral chromatic aberration: each colour is magnified a little differently, red outwards and blue inwards,\n // in proportion to the distance from the centre, which is how a lens's dispersion moves them.\n if (lens.aberration > 0.0) {\n let centre = size * 0.5;\n let away = position - centre;\n let red = imageBilinear(centre + away * (1.0 - lens.aberration));\n let blue = imageBilinear(centre + away * (1.0 + lens.aberration));\n\n color = vec3<f32>(red.r, color.g, blue.b);\n }\n\n if (lens.intensity > 0.0) {\n // The largest folded level is already a tent over the levels below it, so one bilinear read of it is smooth.\n let halo = upBilinear(lens.haloOffset, lens.haloSize, position * vec2<f32>(lens.haloSize) / size);\n\n color = mix(color, halo, lens.intensity);\n }\n\n if (lens.glare > 0.0) {\n color = mix(color, streakBilinear(position * vec2<f32>(lens.streakSize) / size), lens.glare);\n }\n\n if (lens.autoExposure > 0.0) {\n color = color * metered[0].x;\n }\n\n let balanced = vec3<f32>(dot(lens.white0.xyz, color), dot(lens.white1.xyz, color), dot(lens.white2.xyz, color));\n\n graded[id.y * lens.resolution.x + id.x] = vec4<f32>(max(balanced, vec3<f32>(0.0)), 1.0);\n}\n";
|
|
17
17
|
//# sourceMappingURL=lens.wgsl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lens.wgsl.d.ts","sourceRoot":"","sources":["../../src/wgsl/lens.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"lens.wgsl.d.ts","sourceRoot":"","sources":["../../src/wgsl/lens.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,SAAS,gldAuYrB,CAAC"}
|
package/dist/wgsl/lens.wgsl.js
CHANGED
|
@@ -46,7 +46,8 @@ struct Lens {
|
|
|
46
46
|
// The size of the level the rays are gathered at, and the angle of the first ray, in radians.
|
|
47
47
|
streakSize: vec2<u32>,
|
|
48
48
|
glareAngle: f32,
|
|
49
|
-
|
|
49
|
+
// Whether the camera meters the image and exposes it to middle grey, which the meter pass works out.
|
|
50
|
+
autoExposure: f32,
|
|
50
51
|
};
|
|
51
52
|
|
|
52
53
|
const PI: f32 = 3.14159265358979;
|
|
@@ -61,6 +62,108 @@ const STREAK_TAPS: i32 = 24;
|
|
|
61
62
|
@group(0) @binding(4) var<storage, read_write> up: array<vec4<f32>>;
|
|
62
63
|
@group(0) @binding(5) var<storage, read_write> graded: array<vec4<f32>>;
|
|
63
64
|
@group(0) @binding(6) var<storage, read_write> streaked: array<vec4<f32>>;
|
|
65
|
+
// What the camera's meter read: the exposure that brings the image's average to middle grey, in x.
|
|
66
|
+
@group(0) @binding(7) var<storage, read_write> metered: array<vec4<f32>>;
|
|
67
|
+
|
|
68
|
+
/** What a light meter calibrates to: an average scene reflects about eighteen percent. */
|
|
69
|
+
const MIDDLE_GREY: f32 = 0.18;
|
|
70
|
+
const METER_GRID: vec2<u32> = vec2<u32>(64u, 36u);
|
|
71
|
+
const METER_THREADS: u32 = 256u;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* How far above middle grey the meter lets the bright end of the image land: three stops, where AgX still shows
|
|
75
|
+
* detail, three and a half under where it reaches white.
|
|
76
|
+
*/
|
|
77
|
+
const HIGHLIGHT_CEILING: f32 = 8.0;
|
|
78
|
+
/** The share of the metered points that counts as the bright end: the light through a window, not a lamp's own pixels. */
|
|
79
|
+
const HIGHLIGHT_SHARE: f32 = 0.03;
|
|
80
|
+
/** Half stops of luminance, from 2^-16 to 2^16, which the bright end is found in. */
|
|
81
|
+
const METER_BINS: u32 = 64u;
|
|
82
|
+
|
|
83
|
+
var<workgroup> meterSums: array<vec2<f32>, 256>;
|
|
84
|
+
var<workgroup> meterHistogram: array<atomic<u32>, 64>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The camera's meter: the image's average luminance over a grid of points, taken as the mean of the logarithms so that
|
|
88
|
+
* a bright window or a patch of sky cannot darken everything else, weighed towards the centre as a camera's
|
|
89
|
+
* centre-weighted metering is. What no path has reached yet, or is black, does not count. One workgroup reads the
|
|
90
|
+
* grid and sums it in shared memory.
|
|
91
|
+
*
|
|
92
|
+
* Then it guards the highlights, as a camera's matrix metering does: the average alone takes a dim attic with a shaft
|
|
93
|
+
* of sun in it to middle grey and burns the shaft out, where a photographer keeps the room dim. The brightest few
|
|
94
|
+
* percent of the points, found in a histogram of half stops, may land no more than {@link HIGHLIGHT_CEILING} times
|
|
95
|
+
* brighter; an even scene, whose bright end is close to its average, is left as the average says.
|
|
96
|
+
*/
|
|
97
|
+
@compute @workgroup_size(256, 1, 1)
|
|
98
|
+
fn meter(@builtin(local_invocation_index) thread: u32) {
|
|
99
|
+
var sum = vec2<f32>(0.0);
|
|
100
|
+
let count = METER_GRID.x * METER_GRID.y;
|
|
101
|
+
|
|
102
|
+
if (thread < METER_BINS) {
|
|
103
|
+
atomicStore(&meterHistogram[thread], 0u);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
workgroupBarrier();
|
|
107
|
+
|
|
108
|
+
for (var k = thread; k < count; k = k + METER_THREADS) {
|
|
109
|
+
let cell = vec2<f32>(f32(k % METER_GRID.x), f32(k / METER_GRID.x)) + 0.5;
|
|
110
|
+
let uv = cell / vec2<f32>(METER_GRID);
|
|
111
|
+
let luminance = dot(imageTexel(i32(uv.x * f32(lens.resolution.x)), i32(uv.y * f32(lens.resolution.y))),
|
|
112
|
+
vec3<f32>(0.2126, 0.7152, 0.0722));
|
|
113
|
+
let away = (uv - 0.5) * 2.0;
|
|
114
|
+
let weight = select(0.0, 1.0 - 0.6 * min(dot(away, away) * 0.5, 1.0), luminance > 0.0);
|
|
115
|
+
|
|
116
|
+
sum = sum + vec2<f32>(log(max(luminance, 1e-6)) * weight, weight);
|
|
117
|
+
|
|
118
|
+
if (luminance > 0.0) {
|
|
119
|
+
let bin = u32(clamp((log2(luminance) + 16.0) * 2.0, 0.0, f32(METER_BINS - 1u)));
|
|
120
|
+
|
|
121
|
+
atomicAdd(&meterHistogram[bin], 1u);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
meterSums[thread] = sum;
|
|
126
|
+
workgroupBarrier();
|
|
127
|
+
|
|
128
|
+
for (var stride = METER_THREADS / 2u; stride > 0u; stride = stride / 2u) {
|
|
129
|
+
if (thread < stride) {
|
|
130
|
+
meterSums[thread] = meterSums[thread] + meterSums[thread + stride];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
workgroupBarrier();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (thread == 0u) {
|
|
137
|
+
let total = meterSums[0];
|
|
138
|
+
let average = select(MIDDLE_GREY, exp(total.x / max(total.y, 1e-6)), total.y > 0.0);
|
|
139
|
+
var exposure = MIDDLE_GREY / average;
|
|
140
|
+
|
|
141
|
+
// From the top down, the first half stop by which the bright share is reached; its upper edge is that end.
|
|
142
|
+
var points = 0u;
|
|
143
|
+
|
|
144
|
+
for (var bin = 0u; bin < METER_BINS; bin = bin + 1u) {
|
|
145
|
+
points = points + atomicLoad(&meterHistogram[bin]);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
var above = 0u;
|
|
149
|
+
var bright = 0.0;
|
|
150
|
+
|
|
151
|
+
for (var bin = i32(METER_BINS) - 1; bin >= 0 && points > 0u; bin = bin - 1) {
|
|
152
|
+
above = above + atomicLoad(&meterHistogram[u32(bin)]);
|
|
153
|
+
|
|
154
|
+
if (f32(above) >= HIGHLIGHT_SHARE * f32(points)) {
|
|
155
|
+
bright = exp2(f32(bin + 1) * 0.5 - 16.0);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (bright > 0.0) {
|
|
161
|
+
exposure = min(exposure, HIGHLIGHT_CEILING * MIDDLE_GREY / bright);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
metered[0] = vec4<f32>(clamp(exposure, 1e-4, 1e4), average, bright, 0.0);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
64
167
|
|
|
65
168
|
/** The image's own texel: its mean so far, or nothing where no path has landed yet. */
|
|
66
169
|
fn imageTexel(x: i32, y: i32) -> vec3<f32> {
|
|
@@ -260,21 +363,6 @@ fn streakBilinear(position: vec2<f32>) -> vec3<f32> {
|
|
|
260
363
|
return mix(mix(streakTexel(x, y), streakTexel(x + 1, y), b.z), mix(streakTexel(x, y + 1), streakTexel(x + 1, y + 1), b.z), b.w);
|
|
261
364
|
}
|
|
262
365
|
|
|
263
|
-
/** The folded halo at full resolution: a tent over its largest level, which is half the image's size. */
|
|
264
|
-
fn tentHalo(position: vec2<f32>) -> vec3<f32> {
|
|
265
|
-
var sum = vec3<f32>(0.0);
|
|
266
|
-
|
|
267
|
-
for (var dy = -1; dy <= 1; dy = dy + 1) {
|
|
268
|
-
for (var dx = -1; dx <= 1; dx = dx + 1) {
|
|
269
|
-
let weight = f32((2 - abs(dx)) * (2 - abs(dy))) / 16.0;
|
|
270
|
-
|
|
271
|
-
sum = sum + upBilinear(lens.haloOffset, lens.haloSize, position + vec2<f32>(f32(dx), f32(dy)) * 0.5) * weight;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
return sum;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
366
|
/** The image as the lens delivers it: fringed towards the corners, with its halo, balanced for the camera's white. */
|
|
279
367
|
@compute @workgroup_size(8, 8, 1)
|
|
280
368
|
fn composite(@builtin(global_invocation_id) id: vec3<u32>) {
|
|
@@ -298,7 +386,8 @@ fn composite(@builtin(global_invocation_id) id: vec3<u32>) {
|
|
|
298
386
|
}
|
|
299
387
|
|
|
300
388
|
if (lens.intensity > 0.0) {
|
|
301
|
-
|
|
389
|
+
// The largest folded level is already a tent over the levels below it, so one bilinear read of it is smooth.
|
|
390
|
+
let halo = upBilinear(lens.haloOffset, lens.haloSize, position * vec2<f32>(lens.haloSize) / size);
|
|
302
391
|
|
|
303
392
|
color = mix(color, halo, lens.intensity);
|
|
304
393
|
}
|
|
@@ -307,6 +396,10 @@ fn composite(@builtin(global_invocation_id) id: vec3<u32>) {
|
|
|
307
396
|
color = mix(color, streakBilinear(position * vec2<f32>(lens.streakSize) / size), lens.glare);
|
|
308
397
|
}
|
|
309
398
|
|
|
399
|
+
if (lens.autoExposure > 0.0) {
|
|
400
|
+
color = color * metered[0].x;
|
|
401
|
+
}
|
|
402
|
+
|
|
310
403
|
let balanced = vec3<f32>(dot(lens.white0.xyz, color), dot(lens.white1.xyz, color), dot(lens.white2.xyz, color));
|
|
311
404
|
|
|
312
405
|
graded[id.y * lens.resolution.x + id.x] = vec4<f32>(max(balanced, vec3<f32>(0.0)), 1.0);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lens.wgsl.js","sourceRoot":"","sources":["../../src/wgsl/lens.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC
|
|
1
|
+
{"version":3,"file":"lens.wgsl.js","sourceRoot":"","sources":["../../src/wgsl/lens.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuYnC,CAAC"}
|
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
* The falloff, the spot cone and the rectangle's projected area follow `material.js` in the CPU renderer, so direct
|
|
7
7
|
* light from an analytic light agrees with it.
|
|
8
8
|
*/
|
|
9
|
-
export declare const LIGHTS_WGSL = "\nstruct LightSample {\n // Unit vector from the shaded point towards the light.\n direction: vec3<f32>,\n // How far an occlusion ray may travel before it is past the light.\n distance: f32,\n // What the light delivers, already divided by the density it was sampled with.\n value: vec3<f32>,\n // Solid angle density of this direction, for the power heuristic. Zero when no bounce ray could find this light,\n // which is every light that occupies no geometry, and then the light strategy keeps the whole contribution.\n misPdf: f32,\n valid: bool,\n occlude: bool,\n kind: u32,\n};\n\nfn noLight() -> LightSample {\n var sample: LightSample;\n sample.valid = false;\n\n return sample;\n}\n\n/** The light whose cumulative probability range contains u. */\nfn lightByProbability(u: f32) -> Light {\n var low = 0u;\n var high = frame.lightCount - 1u;\n\n loop {\n if (low >= high) {\n break;\n }\n\n let middle = (low + high) / 2u;\n\n // The probabilities packed tight in the texture buffer: the same floats as lights[].cdf, a word apart.\n if (extraFloat(frame.lightCdf + middle) > u) {\n high = middle;\n } else {\n low = middle + 1u;\n }\n }\n\n return lights[low];\n}\n\nconst TREE_NODE_WORDS: u32 = 16u;\nconst TREE_LEAF: u32 = 0x80000000u;\nconst TREE_ROOT_PARENT: u32 = 0xffffffffu;\nconst NO_LIGHT: u32 = 0xffffffffu;\n\n/**\n * How much the lights under a node of the light tree can deliver to a point: their power over the square of the\n * distance, times how well their cone of directions and the receiving surface face each other, each widened by the\n * angle the node's box takes from the point (Conty Estevez and Kulla's importance). A point inside the box rules\n * nothing out. A zero normal is a receiver that takes light from everywhere, such as a point in a volume.\n */\nfn lightNodeImportance(node: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {\n let at = frame.lightTree + node * TREE_NODE_WORDS;\n let low = extraVec4(at);\n let high = extraVec4(at + 4u);\n let cone = extraVec4(at + 8u);\n\n if (low.w <= 0.0) {\n return 0.0;\n }\n\n let centre = (low.xyz + high.xyz) * 0.5;\n let radius = length(high.xyz - low.xyz) * 0.5;\n let offset = position - centre;\n let distance2 = dot(offset, offset);\n\n if (distance2 <= radius * radius) {\n return low.w / max(radius * radius, 1e-12);\n }\n\n let distance = sqrt(distance2);\n let spread = asin(clamp(radius / distance, 0.0, 1.0));\n var orientation = 1.0;\n\n // A cone of minus one is the whole sphere: such lights shine towards every point alike.\n if (high.w > -1.0) {\n let theta = acos(clamp(dot(cone.xyz, offset / distance), -1.0, 1.0));\n let past = max(0.0, theta - acos(high.w) - spread);\n\n orientation = select(0.0, cos(past), past <= acos(clamp(cone.w, -1.0, 1.0)));\n }\n\n var facing = 1.0;\n\n if (any(normal != vec3<f32>(0.0))) {\n let past = max(0.0, acos(clamp(dot(normal, -offset / distance), -1.0, 1.0)) - spread);\n\n facing = select(0.0, cos(past), past < 0.5 * PI);\n }\n\n return low.w * orientation * facing / distance2;\n}\n\n/** The chance of stepping from a node of the tree to each of its children, from this point. */\nfn lightChildOdds(left: u32, right: u32, position: vec3<f32>, normal: vec3<f32>) -> vec2<f32> {\n var importance = vec2<f32>(0.0);\n\n // One loop over the two children, so the importance is written into the kernel once per walk.\n for (var k = 0u; k < 2u; k = k + 1u) {\n importance[k] = lightNodeImportance(select(right, left, k == 0u), position, normal);\n }\n\n let total = importance.x + importance.y;\n\n return select(importance / total, vec2<f32>(0.0), total <= 0.0);\n}\n\n/** A light picked down the tree by importance from this point, with the chance of picking it; NO_LIGHT for none. */\nfn sampleLightTree(position: vec3<f32>, normal: vec3<f32>, u: f32, pdf: ptr<function, f32>,\n density: ptr<function, f32>) -> u32 {\n var node = 0u;\n var chance = 1.0;\n var x = u;\n\n for (var depth = 0u; depth < 64u; depth = depth + 1u) {\n let at = frame.lightTree + node * TREE_NODE_WORDS;\n let first = textures[at + 12u];\n\n if ((first & TREE_LEAF) != 0u) {\n *pdf = chance;\n *density = extraFloat(at + 13u);\n\n return first & ~TREE_LEAF;\n }\n\n let second = textures[at + 13u];\n let odds = lightChildOdds(first, second, position, normal);\n\n if (odds.x + odds.y <= 0.0) {\n return NO_LIGHT;\n }\n\n if (x < odds.x) {\n node = first;\n chance = chance * odds.x;\n x = x / odds.x;\n } else {\n node = second;\n chance = chance * odds.y;\n x = (x - odds.x) / max(odds.y, 1e-20);\n }\n\n x = min(x, 0.99999994);\n }\n\n return NO_LIGHT;\n}\n\n/** The chance the tree would have picked the local light at this index from this point, walked up from its leaf. */\nfn lightTreePdf(index: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {\n var node = textures[frame.lightLeaves + index];\n var chance = extraFloat(frame.lightTree + node * TREE_NODE_WORDS + 13u);\n\n for (var depth = 0u; depth < 64u; depth = depth + 1u) {\n let parent = textures[frame.lightTree + node * TREE_NODE_WORDS + 14u];\n\n if (parent == TREE_ROOT_PARENT) {\n return chance;\n }\n\n let at = frame.lightTree + parent * TREE_NODE_WORDS;\n let first = textures[at + 12u];\n let odds = lightChildOdds(first, textures[at + 13u], position, normal);\n\n chance = chance * select(odds.y, odds.x, node == first);\n node = parent;\n }\n\n return 0.0;\n}\n\n/**\n * The area density with which direct sampling from this point would have picked a point on this triangle, or zero if\n * it never would. Only the leading entries are emissive triangles, and they are in triangle order, so one binary search\n * finds the entry; the tree, when there is one, says how likely it was from here.\n */\nfn lightPdfOfTriangle(triangle: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {\n var low = 0u;\n var high = frame.areaLightCount;\n\n loop {\n if (low >= high) {\n break;\n }\n\n let middle = (low + high) / 2u;\n let candidate = textures[frame.lightTriangles + middle];\n\n if (candidate == triangle) {\n if (frame.lightTree != 0u) {\n return frame.localLightPick * lightTreePdf(middle, position, normal);\n }\n\n return lights[middle].pdf;\n }\n\n if (candidate < triangle) {\n low = middle + 1u;\n } else {\n high = middle;\n }\n }\n\n return 0.0;\n}\n\n/** The power heuristic: how much of a contribution the strategy that produced it keeps, against the other one. */\nfn misWeight(pdf: f32, other: f32) -> f32 {\n let a = pdf * pdf;\n let b = other * other;\n\n return select(a / (a + b), 0.0, a + b <= 0.0);\n}\n\n/**\n * Spreads a direction over a cone, which is how a light of some apparent size softens its own shadow. The settings give\n * the angular diameter; a rasterizer would blur the shadow map by the same amount.\n */\nfn spreadDirection(direction: vec3<f32>, u: vec2<f32>) -> vec3<f32> {\n if (frame.shadowSpread <= 0.0) {\n return direction;\n }\n\n let basis = orthonormalBasis(direction);\n let radius = sqrt(u.x) * frame.shadowSpread;\n let angle = 2.0 * PI * u.y;\n\n return normalize(direction + basis[0] * (radius * cos(angle)) + basis[1] * (radius * sin(angle)));\n}\n\n/** Three's getDistanceAttenuation: the inverse power law, cut off smoothly at the light's range. */\nfn distanceFalloff(light: Light, distance: f32) -> f32 {\n var falloff = 1.0 / max(pow(distance, light.decay), 0.01);\n\n if (light.cutoff > 0.0) {\n let edge = clamp(1.0 - pow(distance / light.cutoff, 4.0), 0.0, 1.0);\n\n falloff = falloff * edge * edge;\n }\n\n return falloff;\n}\n\n/** A point uniformly distributed over a rectangular light, and the axis it emits along. */\nfn rectanglePoint(light: Light, u: vec2<f32>) -> vec3<f32> {\n return light.position + light.tangent * (u.x * 2.0 - 1.0) + light.bitangent * (u.y * 2.0 - 1.0);\n}\n\n/**\n * Where the light that pick selects arrives from, and how much of it. u places the point on a surface light or spreads a\n * point light's shadow, so the same two dimensions serve every kind.\n */\nfn sampleLight(position: vec3<f32>, normal: vec3<f32>, pick: f32, u: vec2<f32>) -> LightSample {\n if (frame.lightCount == 0u) {\n return noLight();\n }\n\n // The lights in the scene go through the tree, by what they can deliver here; what lies outside it by its weight.\n if (frame.lightTree != 0u && pick < frame.localLightPick) {\n var chance = 0.0;\n var density = 1.0;\n let index = sampleLightTree(position, normal, pick / frame.localLightPick, &chance, &density);\n\n if (index == NO_LIGHT || chance <= 0.0) {\n return noLight();\n }\n\n return sampleLightEntry(lights[index], frame.localLightPick * chance * density, position, u);\n }\n\n let light = lightByProbability(pick);\n\n return sampleLightEntry(light, light.pdf, position, u);\n}\n\n/** Three's spotLightMap: inside the projection's frustum the texture tints the light; outside it leaves it alone. */\nfn spotLightTint(light: Light, position: vec3<f32>) -> vec3<f32> {\n if ((light.flags & LIGHT_HAS_MAP) == 0u) {\n return vec3<f32>(1.0);\n }\n\n let at = light.triangle;\n let map = textures[at];\n let matrix = mat4x4<f32>(extraVec4(at + 1u), extraVec4(at + 5u), extraVec4(at + 9u), extraVec4(at + 13u));\n let coordinate = matrix * vec4<f32>(position, 1.0);\n\n if (coordinate.w <= 0.0) {\n return vec3<f32>(1.0);\n }\n\n let projected = coordinate.xyz / coordinate.w;\n\n if (any(projected <= vec3<f32>(0.0)) || any(projected >= vec3<f32>(1.0))) {\n return vec3<f32>(1.0);\n }\n\n return sampleTextureRaw(map, projected.xy).rgb;\n}\n\n/** pdf is the chance of picking this light, times the area density of a point on it where it has an area. */\nfn sampleLightEntry(light: Light, pdf: f32, position: vec3<f32>, u: vec2<f32>) -> LightSample {\n var sample: LightSample;\n sample.valid = true;\n sample.kind = light.kind;\n // Emissive geometry is always tested: a panel that lit the room through its own wall would not be a path tracer.\n sample.occlude = light.kind == LIGHT_AREA\n || ((light.flags & LIGHT_CASTS_SHADOW) != 0u && frame.shadowsEnabled != 0u);\n sample.misPdf = 0.0;\n\n if (light.kind == LIGHT_ENVIRONMENT) {\n let drawn = sampleEnvironmentDirection(u);\n\n if (drawn.pdf <= 0.0) {\n return noLight();\n }\n\n let density = pdf * drawn.pdf;\n\n sample.direction = drawn.direction;\n sample.distance = 1e30;\n sample.value = environmentRadiance(drawn.direction) / density;\n // A bounce ray that escapes finds the same light, so the two strategies share it.\n sample.misPdf = density;\n\n return sample;\n }\n\n if (light.kind == LIGHT_DIRECTIONAL) {\n sample.direction = spreadDirection(light.direction, u);\n sample.distance = 1e30;\n // The colour already carries the intensity, which for a directional light is the irradiance it delivers.\n sample.value = light.color / pdf;\n\n return sample;\n }\n\n if (light.kind == LIGHT_AREA || light.kind == LIGHT_RECT) {\n let area = light.kind == LIGHT_AREA;\n let bary = triangleBarycentric(u);\n let point = select(rectanglePoint(light, u), trianglePointAt(light.triangle, bary), area);\n let axis = select(normalize(cross(light.tangent, light.bitangent)), triangleNormal(light.triangle), area);\n let offset = point - position;\n let distanceSquared = dot(offset, offset);\n let distance = sqrt(distanceSquared);\n\n if (distance <= RAY_EPSILON) {\n return noLight();\n }\n\n sample.direction = offset / distance;\n // An emissive triangle radiates from both sides in this milestone; a rectangular light only from the side it faces.\n let cosine = dot(axis, sample.direction);\n let cosLight = select(max(cosine, 0.0), abs(cosine), area);\n\n if (cosLight <= 1e-6) {\n return noLight();\n }\n\n // Stop just short of the light so its own surface is not reported as the blocker.\n sample.distance = distance * (1.0 - 1e-3);\n let density = pdf * distanceSquared / cosLight;\n // The emissive map is read at the very point that was sampled, so this agrees with what a bounce ray would find.\n let emission = select(light.color, emissionAt(light.triangle, bary), area);\n\n sample.value = emission / density;\n // Only a triangle of the scene's geometry can also be found by a bounce ray, so only it needs the heuristic.\n sample.misPdf = select(0.0, density, area);\n\n return sample;\n }\n\n // A point or a spot light: both fall off with distance and are sampled at their centre.\n let offset = light.position - position;\n let distance = length(offset);\n\n if (distance <= RAY_EPSILON) {\n return noLight();\n }\n\n let towards = offset / distance;\n var power = distanceFalloff(light, distance);\n\n if (light.kind == LIGHT_SPOT) {\n let cosine = dot(light.direction, towards);\n\n if (light.innerCos <= light.outerCos) {\n power = power * select(0.0, 1.0, cosine >= light.outerCos);\n } else {\n let t = clamp((cosine - light.outerCos) / (light.innerCos - light.outerCos), 0.0, 1.0);\n\n power = power * t * t * (3.0 - 2.0 * t);\n }\n\n if (power <= 0.0) {\n return noLight();\n }\n }\n\n sample.direction = spreadDirection(towards, u);\n sample.distance = distance;\n sample.value = light.color * (power / pdf);\n\n if (light.kind == LIGHT_SPOT) {\n sample.value = sample.value * spotLightTint(light, position);\n }\n\n return sample;\n}\n\n/**\n * Whether a material stops every shadow ray that meets it, wherever: opaque, no cutout, no transmission, no clipping\n * or wireframe, no shader that might discard, and normal blending. Only then can the first triangle found decide.\n */\nfn blocksAll(m: u32) -> bool {\n let flags = materials[m].flags;\n let blending = materials[m].blending;\n\n return (flags & (MATERIAL_TRANSPARENT | MATERIAL_ALPHA_HASH | MATERIAL_WIREFRAME | MATERIAL_VOLUME)) == 0u\n && materials[m].alphaTest <= 0.0\n && materials[m].transmission <= 0.0\n && materials[m].clipCount == 0u\n && materials[m].program == NO_PROGRAM\n && materials[m].kind != KIND_SHADOW\n && (blending == BLEND_NORMAL || blending == BLEND_NONE);\n}\n\n/** How many surfaces a shadow ray may pass through before it gives up and calls the light blocked. */\nconst MAX_SHADOW_SKIPS: u32 = 8u;\n\n/** No material to skip: every blocker counts. */\nconst NO_MATERIAL: u32 = 0xffffffffu;\n\n/**\n * What a shadow ray keeps of a thin transmissive wall it meets: the share Three lets through, tinted by the base colour,\n * less what the interface reflects. It is the expectation of the straight crossing in sampleDielectric, so a light\n * seen through a window is found the same by both estimators. A volume bends the ray it lets through, so it blocks.\n */\nfn thinCrossing(triangle: u32, bary: vec2<f32>, direction: vec3<f32>) -> vec3<f32> {\n let m = materialIndexOf(triangle);\n\n if (materials[m].transmission <= 0.0) {\n return vec3<f32>(0.0);\n }\n\n let metalness = materials[m].metalness * sampleMap(materials[m].metalnessMap, triangle, bary).b;\n let transmission = transmissionAt(m, triangle, bary, metalness);\n let thickness = materials[m].thickness * sampleMap(materials[m].thicknessMap, triangle, bary).g;\n\n if (transmission <= 0.0 || thickness > 0.0) {\n return vec3<f32>(0.0);\n }\n\n let albedo = materials[m].color.rgb * sampleMap(materials[m].map, triangle, bary).rgb;\n let reflectance = fresnelDielectric(abs(dot(authoredNormal(triangle, bary), direction)), 1.0 / max(materials[m].ior, 1.0));\n\n return albedo * (transmission * (1.0 - reflectance));\n}\n\n/** The colour a blended surface filters what is behind it by: its base colour, with the map and vertex colours. */\nfn blendColor(triangle: u32, bary: vec2<f32>) -> vec3<f32> {\n let m = materialIndexOf(triangle);\n var color = materials[m].color.rgb * sampleMap(materials[m].map, triangle, bary).rgb;\n\n if ((materials[m].flags & MATERIAL_VERTEX_COLORS) != 0u) {\n color = color * triangleColor(triangle, bary).rgb;\n }\n\n return color;\n}\n\n/**\n * How much of the light this sample came from reaches the shaded point. A surface that only partly covers its triangle\n * blocks the ray only that often, which is what makes a cutout leaf cast a cutout shadow; a thin transmissive wall\n * lets its own share through, tinted. A volume's boundary lets the ray in or out, and the medium inside takes its\n * share over the distance between; medium is the density at the shaded point itself, zero outside any volume.\n */\nfn lightTransmittance(position: vec3<f32>, chosen: LightSample, rng: ptr<function, PathSampler>, skipMaterial: u32,\n medium: f32) -> vec3<f32> {\n // A light that casts no shadow is blocked by nothing, but a medium on the way still takes its share: then the walk\n // below only looks for volumes and passes every other surface.\n if (!chosen.occlude && !VOLUMES) {\n return vec3<f32>(1.0);\n }\n\n if (chosen.occlude) {\n // Most blockers are plainly opaque, and for those any triangle on the way settles it: the search stops at the\n // first one found instead of looking for the closest. Anything that lets light through goes the long way below.\n let first = traceAny(position, chosen.direction, chosen.distance);\n\n if (first == NO_HIT) {\n return vec3<f32>(exp(-medium * chosen.distance));\n }\n\n let firstMaterial = materialIndexOf(first);\n\n if (firstMaterial != skipMaterial && blocksAll(firstMaterial)) {\n return vec3<f32>(0.0);\n }\n }\n\n var origin = position;\n var remaining = chosen.distance;\n var through = vec3<f32>(1.0);\n // The medium the ray is in, and how far along the ray it entered it.\n var density = medium;\n var travelled = 0.0;\n var enteredAt = 0.0;\n\n for (var skip = 0u; skip < MAX_SHADOW_SKIPS; skip = skip + 1u) {\n let blocker = traceScene(origin, chosen.direction, remaining);\n\n if (blocker.triangle == NO_HIT) {\n return through * exp(-density * (travelled + remaining - enteredAt));\n }\n\n let bary = vec2<f32>(blocker.u, blocker.v);\n let m = materialIndexOf(blocker.triangle);\n let blending = materials[m].blending;\n\n if (VOLUMES && (materials[m].flags & MATERIAL_VOLUME) != 0u) {\n let at = travelled + blocker.distance;\n\n if (dot(authoredNormal(blocker.triangle, bary), chosen.direction) < 0.0) {\n density = select(0.0, materials[m].volume.w, frame.volumesEnabled != 0u);\n enteredAt = at;\n } else {\n through = through * exp(-density * (at - enteredAt));\n density = 0.0;\n }\n } else if (chosen.occlude && m != skipMaterial && materials[m].kind != KIND_SHADOW && blending != BLEND_ADDITIVE) {\n let coverage = coverageAt(blocker.triangle, bary, chosen.direction);\n\n if (blending == BLEND_MULTIPLY) {\n through = through * mix(vec3<f32>(1.0), blendColor(blocker.triangle, bary), coverage);\n } else if (blending == BLEND_SUBTRACTIVE) {\n through = through * (vec3<f32>(1.0) - blendColor(blocker.triangle, bary) * coverage);\n } else if (coverage >= 1.0 || sample2D(rng).x < coverage) {\n through = through * thinCrossing(blocker.triangle, bary, chosen.direction);\n }\n\n if (all(through <= vec3<f32>(0.0))) {\n return vec3<f32>(0.0);\n }\n }\n\n // The ray carries on from just past the surface it saw through.\n origin = origin + chosen.direction * (blocker.distance + RAY_EPSILON);\n remaining = remaining - blocker.distance - RAY_EPSILON;\n travelled = travelled + blocker.distance + RAY_EPSILON;\n\n if (remaining <= RAY_EPSILON) {\n return through * exp(-density * (travelled - enteredAt));\n }\n }\n\n // Too many surfaces to see through: a shadow ray gives up dark, a ray that only counts the media keeps what it has.\n return select(vec3<f32>(0.0), through, !chosen.occlude);\n}\n\n/**\n * How shadowed a point is, as Three's ShadowMaterial reads it: for every light that casts a shadow, whether something\n * stands between it and the point, the darkest of them winning as in the CPU renderer. A rectangular light casts no\n * shadow in Three, so it takes no part.\n */\nfn shadowMaskAt(position: vec3<f32>, rng: ptr<function, PathSampler>) -> f32 {\n if (frame.shadowsEnabled == 0u) {\n return 0.0;\n }\n\n var shadow = 0.0;\n\n for (var i = frame.areaLightCount; i < frame.lightCount; i = i + 1u) {\n let light = lights[i];\n\n if (light.kind == LIGHT_ENVIRONMENT || light.kind == LIGHT_RECT || (light.flags & LIGHT_CASTS_SHADOW) == 0u) {\n continue;\n }\n\n let chosen = sampleLightEntry(light, light.pdf, position, sample2D(rng));\n\n if (!chosen.valid) {\n continue;\n }\n\n let visible = luminanceOf(lightTransmittance(position, chosen, rng, NO_MATERIAL, 0.0));\n\n shadow = max(shadow, 1.0 - clamp(visible, 0.0, 1.0));\n }\n\n return shadow;\n}\n";
|
|
9
|
+
export declare const LIGHTS_WGSL = "\nstruct LightSample {\n // Unit vector from the shaded point towards the light.\n direction: vec3<f32>,\n // How far an occlusion ray may travel before it is past the light.\n distance: f32,\n // What the light delivers, already divided by the density it was sampled with.\n value: vec3<f32>,\n // Solid angle density of this direction, for the power heuristic. Zero when no bounce ray could find this light,\n // which is every light that occupies no geometry, and then the light strategy keeps the whole contribution.\n misPdf: f32,\n valid: bool,\n occlude: bool,\n kind: u32,\n};\n\nfn noLight() -> LightSample {\n var sample: LightSample;\n sample.valid = false;\n\n return sample;\n}\n\n/** The light whose cumulative probability range contains u. */\nfn lightByProbability(u: f32) -> Light {\n var low = 0u;\n var high = frame.lightCount - 1u;\n\n loop {\n if (low >= high) {\n break;\n }\n\n let middle = (low + high) / 2u;\n\n // The probabilities packed tight in the texture buffer: the same floats as lights[].cdf, a word apart.\n if (extraFloat(frame.lightCdf + middle) > u) {\n high = middle;\n } else {\n low = middle + 1u;\n }\n }\n\n return lights[low];\n}\n\nconst TREE_NODE_WORDS: u32 = 16u;\nconst TREE_LEAF: u32 = 0x80000000u;\nconst TREE_ROOT_PARENT: u32 = 0xffffffffu;\nconst TREE_ENTRY_WORDS: u32 = 4u;\nconst NO_LIGHT: u32 = 0xffffffffu;\n\n/**\n * How much the lights under a node of the light tree can deliver to a point: their power over the square of the\n * distance, times how well their cone of directions and the receiving surface face each other, each widened by the\n * angle the node's box takes from the point (Conty Estevez and Kulla's importance). A point inside the box rules\n * nothing out. A zero normal is a receiver that takes light from everywhere, such as a point in a volume.\n */\nfn lightNodeImportance(tree: u32, node: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {\n let at = tree + node * TREE_NODE_WORDS;\n let low = extraVec4(at);\n let high = extraVec4(at + 4u);\n\n if (low.w <= 0.0) {\n return 0.0;\n }\n\n let centre = (low.xyz + high.xyz) * 0.5;\n let radius2 = dot(high.xyz - low.xyz, high.xyz - low.xyz) * 0.25;\n let offset = position - centre;\n let distance2 = dot(offset, offset);\n\n if (distance2 <= radius2) {\n return low.w / max(radius2, 1e-12);\n }\n\n let away = offset * inverseSqrt(distance2);\n // The angle the box takes from the point, as its sine and cosine: the angles below are widened by it without a\n // single inverse trigonometric function, by the sum and difference formulas.\n let sinU2 = radius2 / distance2;\n let cosU = sqrt(1.0 - sinU2);\n let sinU = sqrt(sinU2);\n var orientation = 1.0;\n\n // A cone of minus one is the whole sphere: such lights shine towards every point alike.\n if (high.w > -1.0) {\n let cone = extraVec4(at + 8u);\n let cosO = high.w;\n let sinO = sqrt(max(0.0, 1.0 - cosO * cosO));\n // The cone widened by the box: cos and sin of theta_o + theta_u, the whole sphere once that passes pi.\n let cosWide = cosO * cosU - sinO * sinU;\n let sinWide = sinO * cosU + cosO * sinU;\n let cosTheta = dot(cone.xyz, away);\n\n // cos(max(0, theta - theta_o - theta_u)), which is one while the point lies within the widened cone.\n if (sinWide >= 0.0 && cosTheta < cosWide) {\n let sinTheta = sqrt(max(0.0, 1.0 - cosTheta * cosTheta));\n let cosPast = cosTheta * cosWide + sinTheta * sinWide;\n\n orientation = select(0.0, cosPast, cosPast >= cone.w);\n }\n }\n\n var facing = 1.0;\n\n if (any(normal != vec3<f32>(0.0))) {\n let cosN = -dot(normal, away);\n\n // cos(max(0, theta_n - theta_u)), and nothing once that reaches a right angle.\n if (cosN < cosU) {\n let sinN = sqrt(max(0.0, 1.0 - cosN * cosN));\n\n facing = max(0.0, cosN * cosU + sinN * sinU);\n }\n }\n\n return low.w * orientation * facing / distance2;\n}\n\n/** The chance of stepping from a node of a tree (the lights' or the windows') to each of its children, from here. */\nfn lightChildOdds(tree: u32, left: u32, right: u32, position: vec3<f32>, normal: vec3<f32>) -> vec2<f32> {\n var importance = vec2<f32>(0.0);\n\n // One loop over the two children, so the importance is written into the kernel once per walk.\n for (var k = 0u; k < 2u; k = k + 1u) {\n importance[k] = lightNodeImportance(tree, select(right, left, k == 0u), position, normal);\n }\n\n let total = importance.x + importance.y;\n\n return select(importance / total, vec2<f32>(0.0), total <= 0.0);\n}\n\n/**\n * An entry picked down a tree by importance from this point, with the chance of picking it; NO_LIGHT for none. The\n * tree is the lights' or the windows', given by where its nodes and its leaves' list start.\n */\nfn sampleLightTree(tree: u32, list: u32, position: vec3<f32>, normal: vec3<f32>, u: f32, pdf: ptr<function, f32>,\n density: ptr<function, f32>) -> u32 {\n var node = 0u;\n var chance = 1.0;\n var x = u;\n\n for (var depth = 0u; depth < 64u; depth = depth + 1u) {\n let at = tree + node * TREE_NODE_WORDS;\n let first = textures[at + 12u];\n\n // A leaf lists its lights, too close together to tell apart from here: one is picked by its share of the power.\n if ((first & TREE_LEAF) != 0u) {\n let start = list + (first & ~TREE_LEAF) * TREE_ENTRY_WORDS;\n let count = textures[at + 13u];\n var slot = start + (count - 1u) * TREE_ENTRY_WORDS;\n\n for (var j = 0u; j + 1u < count; j = j + 1u) {\n if (x < extraFloat(start + j * TREE_ENTRY_WORDS + 3u)) {\n slot = start + j * TREE_ENTRY_WORDS;\n break;\n }\n }\n\n *pdf = chance * extraFloat(slot + 1u);\n *density = extraFloat(slot + 2u);\n\n return textures[slot];\n }\n\n let second = textures[at + 13u];\n let odds = lightChildOdds(tree, first, second, position, normal);\n\n if (odds.x + odds.y <= 0.0) {\n return NO_LIGHT;\n }\n\n if (x < odds.x) {\n node = first;\n chance = chance * odds.x;\n x = x / odds.x;\n } else {\n node = second;\n chance = chance * odds.y;\n x = (x - odds.x) / max(odds.y, 1e-20);\n }\n\n x = min(x, 0.99999994);\n }\n\n return NO_LIGHT;\n}\n\n/** The chance the tree would have picked the local light at this index from this point, walked up from its leaf. */\nfn lightTreePdf(index: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {\n var node = textures[frame.lightLeaves + index * 2u];\n let slot = frame.lightList + textures[frame.lightLeaves + index * 2u + 1u] * TREE_ENTRY_WORDS;\n // Its share within its leaf, and the area density of a point on it.\n var chance = extraFloat(slot + 1u) * extraFloat(slot + 2u);\n\n for (var depth = 0u; depth < 64u; depth = depth + 1u) {\n let parent = textures[frame.lightTree + node * TREE_NODE_WORDS + 14u];\n\n if (parent == TREE_ROOT_PARENT) {\n return chance;\n }\n\n let at = frame.lightTree + parent * TREE_NODE_WORDS;\n let first = textures[at + 12u];\n let odds = lightChildOdds(frame.lightTree, first, textures[at + 13u], position, normal);\n\n chance = chance * select(odds.y, odds.x, node == first);\n node = parent;\n }\n\n return 0.0;\n}\n\n/**\n * The area density with which direct sampling from this point would have picked a point on this triangle, or zero if\n * it never would. Only the leading entries are emissive triangles, and they are in triangle order, so one binary search\n * finds the entry; the tree, when there is one, says how likely it was from here.\n */\nfn lightPdfOfTriangle(triangle: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {\n var low = 0u;\n var high = frame.areaLightCount;\n\n loop {\n if (low >= high) {\n break;\n }\n\n let middle = (low + high) / 2u;\n let candidate = textures[frame.lightTriangles + middle];\n\n if (candidate == triangle) {\n if (frame.lightTree != 0u) {\n return frame.localLightPick * lightTreePdf(middle, position, normal);\n }\n\n return lights[middle].pdf;\n }\n\n if (candidate < triangle) {\n low = middle + 1u;\n } else {\n high = middle;\n }\n }\n\n return 0.0;\n}\n\n/** Share of the sky's samples aimed through the windows, from a point inside a room. */\nconst PORTAL_SHARE: f32 = 0.5;\n\n/**\n * How often the sky is aimed at through the windows from this point: from inside a room, where something stands above\n * it, half the time; outside never, where the open sky is better found by its brightness. A function of the point alone,\n * so the heuristic can ask it again when a bounce ray escapes.\n */\nfn portalChance(position: vec3<f32>) -> f32 {\n if (!PORTALS || frame.portalCount == 0u) {\n return 0.0;\n }\n\n return select(0.0, PORTAL_SHARE, traceAny(position, vec3<f32>(0.0, 1.0, 0.0), 1e30) != NO_HIT);\n}\n\n/** Window subtrees a ray's walk through the windows' tree keeps waiting. */\nconst PORTAL_STACK: u32 = 32u;\n\n/**\n * The solid angle density with which aiming through the windows produces this direction: over every window triangle\n * it crosses, the chance the tree picks that window from here times the area density of a point on it, over the area\n * it shows from here. The ray walks the tree as it would a hierarchy, by the nodes' boxes, carrying the chance of\n * reaching each node, so the windows it misses cost a box test and the ones it crosses no walk back up.\n */\nfn portalPdf(position: vec3<f32>, direction: vec3<f32>) -> f32 {\n var nodes: array<u32, PORTAL_STACK>;\n var chances: array<f32, PORTAL_STACK>;\n var top = 1u;\n let inverse = 1.0 / direction;\n var pdf = 0.0;\n\n nodes[0] = 0u;\n chances[0] = 1.0;\n\n loop {\n if (top == 0u) {\n break;\n }\n\n top = top - 1u;\n\n let node = nodes[top];\n let chance = chances[top];\n let at = frame.portals + node * TREE_NODE_WORDS;\n\n if (enterBox(extraVec4(at).xyz, extraVec4(at + 4u).xyz, position, inverse, 1e30) >= 1e30) {\n continue;\n }\n\n let first = textures[at + 12u];\n let second = textures[at + 13u];\n\n if ((first & TREE_LEAF) != 0u) {\n let start = frame.portalList + (first & ~TREE_LEAF) * TREE_ENTRY_WORDS;\n\n for (var j = 0u; j < second; j = j + 1u) {\n let slot = start + j * TREE_ENTRY_WORDS;\n let triangle = textures[slot];\n let t = intersectTriangle(leafOf(triangle), position, direction).x;\n\n if (t > 0.0) {\n let cosine = abs(dot(triangleNormal(triangle), direction));\n\n pdf = pdf + chance * extraFloat(slot + 1u) * extraFloat(slot + 2u) * t * t / max(cosine, 1e-6);\n }\n }\n\n continue;\n }\n\n let odds = lightChildOdds(frame.portals, first, second, position, vec3<f32>(0.0));\n\n for (var k = 0u; k < 2u; k = k + 1u) {\n if (odds[k] > 0.0 && top < PORTAL_STACK) {\n nodes[top] = select(second, first, k == 0u);\n chances[top] = chance * odds[k];\n top = top + 1u;\n }\n }\n }\n\n return pdf;\n}\n\n/** A direction through a point on a window the tree picks from here, or none from a point on the window itself. */\nfn samplePortal(position: vec3<f32>, u: vec2<f32>) -> vec3<f32> {\n var chance = 0.0;\n var density = 0.0;\n let triangle = sampleLightTree(frame.portals, frame.portalList, position, vec3<f32>(0.0), u.x, &chance, &density);\n\n if (triangle == NO_LIGHT) {\n return vec3<f32>(0.0);\n }\n\n // The tree spent the sample walking down; a fresh one places the point, which the pick does not depend on.\n let point = trianglePointAt(triangle, triangleBarycentric(vec2<f32>(fract(u.x * 4096.0), u.y)));\n let offset = point - position;\n\n return select(vec3<f32>(0.0), normalize(offset), dot(offset, offset) > 1e-12);\n}\n\n/** The density of the sky's sampling from this point for this direction: by brightness, through windows, or both. */\nfn skyPdf(position: vec3<f32>, direction: vec3<f32>, share: f32) -> f32 {\n var pdf = (1.0 - share) * environmentPdf(direction);\n\n if (share > 0.0) {\n pdf = pdf + share * portalPdf(position, direction);\n }\n\n return pdf;\n}\n\n/** The power heuristic: how much of a contribution the strategy that produced it keeps, against the other one. */\nfn misWeight(pdf: f32, other: f32) -> f32 {\n let a = pdf * pdf;\n let b = other * other;\n\n return select(a / (a + b), 0.0, a + b <= 0.0);\n}\n\n/**\n * Spreads a direction over a cone, which is how a light of some apparent size softens its own shadow. The settings give\n * the angular diameter; a rasterizer would blur the shadow map by the same amount.\n */\nfn spreadDirection(direction: vec3<f32>, u: vec2<f32>) -> vec3<f32> {\n if (frame.shadowSpread <= 0.0) {\n return direction;\n }\n\n let basis = orthonormalBasis(direction);\n let radius = sqrt(u.x) * frame.shadowSpread;\n let angle = 2.0 * PI * u.y;\n\n return normalize(direction + basis[0] * (radius * cos(angle)) + basis[1] * (radius * sin(angle)));\n}\n\n/** Three's getDistanceAttenuation: the inverse power law, cut off smoothly at the light's range. */\nfn distanceFalloff(light: Light, distance: f32) -> f32 {\n var falloff = 1.0 / max(pow(distance, light.decay), 0.01);\n\n if (light.cutoff > 0.0) {\n let edge = clamp(1.0 - pow(distance / light.cutoff, 4.0), 0.0, 1.0);\n\n falloff = falloff * edge * edge;\n }\n\n return falloff;\n}\n\n/** A point uniformly distributed over a rectangular light, and the axis it emits along. */\nfn rectanglePoint(light: Light, u: vec2<f32>) -> vec3<f32> {\n return light.position + light.tangent * (u.x * 2.0 - 1.0) + light.bitangent * (u.y * 2.0 - 1.0);\n}\n\n/**\n * Where the light that pick selects arrives from, and how much of it. u places the point on a surface light or spreads a\n * point light's shadow, so the same two dimensions serve every kind.\n */\nfn sampleLight(position: vec3<f32>, normal: vec3<f32>, pick: f32, u: vec2<f32>) -> LightSample {\n if (frame.lightCount == 0u) {\n return noLight();\n }\n\n // The lights in the scene go through the tree, by what they can deliver here; what lies outside it by its weight.\n if (frame.lightTree != 0u && pick < frame.localLightPick) {\n var chance = 0.0;\n var density = 1.0;\n let index = sampleLightTree(frame.lightTree, frame.lightList, position, normal, pick / frame.localLightPick, &chance, &density);\n\n if (index == NO_LIGHT || chance <= 0.0) {\n return noLight();\n }\n\n return sampleLightEntry(lights[index], frame.localLightPick * chance * density, position, u);\n }\n\n let light = lightByProbability(pick);\n\n return sampleLightEntry(light, light.pdf, position, u);\n}\n\n/** Three's spotLightMap: inside the projection's frustum the texture tints the light; outside it leaves it alone. */\nfn spotLightTint(light: Light, position: vec3<f32>) -> vec3<f32> {\n if ((light.flags & LIGHT_HAS_MAP) == 0u) {\n return vec3<f32>(1.0);\n }\n\n let at = light.triangle;\n let map = textures[at];\n let matrix = mat4x4<f32>(extraVec4(at + 1u), extraVec4(at + 5u), extraVec4(at + 9u), extraVec4(at + 13u));\n let coordinate = matrix * vec4<f32>(position, 1.0);\n\n if (coordinate.w <= 0.0) {\n return vec3<f32>(1.0);\n }\n\n let projected = coordinate.xyz / coordinate.w;\n\n if (any(projected <= vec3<f32>(0.0)) || any(projected >= vec3<f32>(1.0))) {\n return vec3<f32>(1.0);\n }\n\n return sampleTextureRaw(map, projected.xy).rgb;\n}\n\n/** pdf is the chance of picking this light, times the area density of a point on it where it has an area. */\nfn sampleLightEntry(light: Light, pdf: f32, position: vec3<f32>, u: vec2<f32>) -> LightSample {\n var sample: LightSample;\n sample.valid = true;\n sample.kind = light.kind;\n // Emissive geometry is always tested: a panel that lit the room through its own wall would not be a path tracer.\n sample.occlude = light.kind == LIGHT_AREA\n || ((light.flags & LIGHT_CASTS_SHADOW) != 0u && frame.shadowsEnabled != 0u);\n sample.misPdf = 0.0;\n\n if (light.kind == LIGHT_ENVIRONMENT) {\n // From inside a room, part of the time through a window: daylight comes in there or not at all.\n let share = portalChance(position);\n var direction = vec3<f32>(0.0);\n\n if (u.x < share) {\n direction = samplePortal(position, vec2<f32>(u.x / share, u.y));\n } else {\n let drawn = sampleEnvironmentDirection(vec2<f32>(min((u.x - share) / (1.0 - share), 0.99999994), u.y));\n\n if (drawn.pdf > 0.0) {\n direction = drawn.direction;\n }\n }\n\n let mixed = skyPdf(position, direction, share);\n\n if (all(direction == vec3<f32>(0.0)) || mixed <= 0.0) {\n return noLight();\n }\n\n let density = pdf * mixed;\n\n sample.direction = direction;\n sample.distance = 1e30;\n sample.value = environmentRadiance(direction) / density;\n // A bounce ray that escapes finds the same light, so the two strategies share it.\n sample.misPdf = density;\n\n return sample;\n }\n\n if (light.kind == LIGHT_DIRECTIONAL) {\n sample.direction = spreadDirection(light.direction, u);\n sample.distance = 1e30;\n // The colour already carries the intensity, which for a directional light is the irradiance it delivers.\n sample.value = light.color / pdf;\n\n return sample;\n }\n\n if (light.kind == LIGHT_AREA || light.kind == LIGHT_RECT) {\n let area = light.kind == LIGHT_AREA;\n let bary = triangleBarycentric(u);\n let point = select(rectanglePoint(light, u), trianglePointAt(light.triangle, bary), area);\n let axis = select(normalize(cross(light.tangent, light.bitangent)), triangleNormal(light.triangle), area);\n let offset = point - position;\n let distanceSquared = dot(offset, offset);\n let distance = sqrt(distanceSquared);\n\n if (distance <= RAY_EPSILON) {\n return noLight();\n }\n\n sample.direction = offset / distance;\n // An emissive triangle radiates from both sides in this milestone; a rectangular light only from the side it faces.\n let cosine = dot(axis, sample.direction);\n let cosLight = select(max(cosine, 0.0), abs(cosine), area);\n\n if (cosLight <= 1e-6) {\n return noLight();\n }\n\n // Stop just short of the light so its own surface is not reported as the blocker.\n sample.distance = distance * (1.0 - 1e-3);\n let density = pdf * distanceSquared / cosLight;\n // The emissive map is read at the very point that was sampled, so this agrees with what a bounce ray would find.\n let emission = select(light.color, emissionAt(light.triangle, bary), area);\n\n sample.value = emission / density;\n // Only a triangle of the scene's geometry can also be found by a bounce ray, so only it needs the heuristic.\n sample.misPdf = select(0.0, density, area);\n\n return sample;\n }\n\n // A point or a spot light: both fall off with distance and are sampled at their centre.\n let offset = light.position - position;\n let distance = length(offset);\n\n if (distance <= RAY_EPSILON) {\n return noLight();\n }\n\n let towards = offset / distance;\n var power = distanceFalloff(light, distance);\n\n if (light.kind == LIGHT_SPOT) {\n let cosine = dot(light.direction, towards);\n\n if (light.innerCos <= light.outerCos) {\n power = power * select(0.0, 1.0, cosine >= light.outerCos);\n } else {\n let t = clamp((cosine - light.outerCos) / (light.innerCos - light.outerCos), 0.0, 1.0);\n\n power = power * t * t * (3.0 - 2.0 * t);\n }\n\n if (power <= 0.0) {\n return noLight();\n }\n }\n\n sample.direction = spreadDirection(towards, u);\n sample.distance = distance;\n sample.value = light.color * (power / pdf);\n\n if (light.kind == LIGHT_SPOT) {\n sample.value = sample.value * spotLightTint(light, position);\n }\n\n return sample;\n}\n\n/**\n * Whether a material stops every shadow ray that meets it, wherever: opaque, no cutout, no transmission, no clipping\n * or wireframe, no shader that might discard, and normal blending. Only then can the first triangle found decide.\n */\nfn blocksAll(m: u32) -> bool {\n let flags = materials[m].flags;\n let blending = materials[m].blending;\n\n return (flags & (MATERIAL_TRANSPARENT | MATERIAL_ALPHA_HASH | MATERIAL_WIREFRAME | MATERIAL_VOLUME)) == 0u\n && materials[m].alphaTest <= 0.0\n && materials[m].transmission <= 0.0\n && materials[m].clipCount == 0u\n && materials[m].program == NO_PROGRAM\n && materials[m].kind != KIND_SHADOW\n && (blending == BLEND_NORMAL || blending == BLEND_NONE);\n}\n\n/** How many surfaces a shadow ray may pass through before it gives up and calls the light blocked. */\nconst MAX_SHADOW_SKIPS: u32 = 8u;\n\n/** No material to skip: every blocker counts. */\nconst NO_MATERIAL: u32 = 0xffffffffu;\n\n/**\n * What a shadow ray keeps of a thin transmissive wall it meets: the share Three lets through, tinted by the base colour,\n * less what the interface reflects. It is the expectation of the straight crossing in sampleDielectric, so a light\n * seen through a window is found the same by both estimators. A volume bends the ray it lets through, so it blocks.\n */\nfn thinCrossing(triangle: u32, bary: vec2<f32>, direction: vec3<f32>) -> vec3<f32> {\n let m = materialIndexOf(triangle);\n\n if (materials[m].transmission <= 0.0) {\n return vec3<f32>(0.0);\n }\n\n let metalness = materials[m].metalness * sampleMap(materials[m].metalnessMap, triangle, bary).b;\n let transmission = transmissionAt(m, triangle, bary, metalness);\n let thickness = materials[m].thickness * sampleMap(materials[m].thicknessMap, triangle, bary).g;\n\n if (transmission <= 0.0 || thickness > 0.0) {\n return vec3<f32>(0.0);\n }\n\n let albedo = materials[m].color.rgb * sampleMap(materials[m].map, triangle, bary).rgb;\n let reflectance = fresnelDielectric(abs(dot(authoredNormal(triangle, bary), direction)), 1.0 / max(materials[m].ior, 1.0));\n\n return albedo * (transmission * (1.0 - reflectance));\n}\n\n/** The colour a blended surface filters what is behind it by: its base colour, with the map and vertex colours. */\nfn blendColor(triangle: u32, bary: vec2<f32>) -> vec3<f32> {\n let m = materialIndexOf(triangle);\n var color = materials[m].color.rgb * sampleMap(materials[m].map, triangle, bary).rgb;\n\n if ((materials[m].flags & MATERIAL_VERTEX_COLORS) != 0u) {\n color = color * triangleColor(triangle, bary).rgb;\n }\n\n return color;\n}\n\n/**\n * How much of the light this sample came from reaches the shaded point. A surface that only partly covers its triangle\n * blocks the ray only that often, which is what makes a cutout leaf cast a cutout shadow; a thin transmissive wall\n * lets its own share through, tinted. A volume's boundary lets the ray in or out, and the medium inside takes its\n * share over the distance between; medium is the density at the shaded point itself, zero outside any volume.\n */\nfn lightTransmittance(position: vec3<f32>, chosen: LightSample, rng: ptr<function, PathSampler>, skipMaterial: u32,\n medium: f32) -> vec3<f32> {\n // A light that casts no shadow is blocked by nothing, but a medium on the way still takes its share: then the walk\n // below only looks for volumes and passes every other surface.\n if (!chosen.occlude && !VOLUMES) {\n return vec3<f32>(1.0);\n }\n\n if (chosen.occlude) {\n // Most blockers are plainly opaque, and for those any triangle on the way settles it: the search stops at the\n // first one found instead of looking for the closest. Anything that lets light through goes the long way below.\n let first = traceAny(position, chosen.direction, chosen.distance);\n\n if (first == NO_HIT) {\n return vec3<f32>(exp(-medium * chosen.distance));\n }\n\n let firstMaterial = materialIndexOf(first);\n\n if (firstMaterial != skipMaterial && blocksAll(firstMaterial)) {\n return vec3<f32>(0.0);\n }\n }\n\n var origin = position;\n var remaining = chosen.distance;\n var through = vec3<f32>(1.0);\n // The medium the ray is in, and how far along the ray it entered it.\n var density = medium;\n var travelled = 0.0;\n var enteredAt = 0.0;\n\n for (var skip = 0u; skip < MAX_SHADOW_SKIPS; skip = skip + 1u) {\n let blocker = traceScene(origin, chosen.direction, remaining);\n\n if (blocker.triangle == NO_HIT) {\n return through * exp(-density * (travelled + remaining - enteredAt));\n }\n\n let bary = vec2<f32>(blocker.u, blocker.v);\n let m = materialIndexOf(blocker.triangle);\n let blending = materials[m].blending;\n\n if (VOLUMES && (materials[m].flags & MATERIAL_VOLUME) != 0u) {\n let at = travelled + blocker.distance;\n\n if (dot(authoredNormal(blocker.triangle, bary), chosen.direction) < 0.0) {\n density = select(0.0, materials[m].volume.w, frame.volumesEnabled != 0u);\n enteredAt = at;\n } else {\n through = through * exp(-density * (at - enteredAt));\n density = 0.0;\n }\n } else if (chosen.occlude && m != skipMaterial && materials[m].kind != KIND_SHADOW && blending != BLEND_ADDITIVE) {\n let coverage = coverageAt(blocker.triangle, bary, chosen.direction);\n\n if (blending == BLEND_MULTIPLY) {\n through = through * mix(vec3<f32>(1.0), blendColor(blocker.triangle, bary), coverage);\n } else if (blending == BLEND_SUBTRACTIVE) {\n through = through * (vec3<f32>(1.0) - blendColor(blocker.triangle, bary) * coverage);\n } else if (coverage >= 1.0 || sample2D(rng).x < coverage) {\n through = through * thinCrossing(blocker.triangle, bary, chosen.direction);\n }\n\n if (all(through <= vec3<f32>(0.0))) {\n return vec3<f32>(0.0);\n }\n }\n\n // The ray carries on from just past the surface it saw through.\n origin = origin + chosen.direction * (blocker.distance + RAY_EPSILON);\n remaining = remaining - blocker.distance - RAY_EPSILON;\n travelled = travelled + blocker.distance + RAY_EPSILON;\n\n if (remaining <= RAY_EPSILON) {\n return through * exp(-density * (travelled - enteredAt));\n }\n }\n\n // Too many surfaces to see through: a shadow ray gives up dark, a ray that only counts the media keeps what it has.\n return select(vec3<f32>(0.0), through, !chosen.occlude);\n}\n\n/**\n * How shadowed a point is, as Three's ShadowMaterial reads it: for every light that casts a shadow, whether something\n * stands between it and the point, the darkest of them winning as in the CPU renderer. A rectangular light casts no\n * shadow in Three, so it takes no part.\n */\nfn shadowMaskAt(position: vec3<f32>, rng: ptr<function, PathSampler>) -> f32 {\n if (frame.shadowsEnabled == 0u) {\n return 0.0;\n }\n\n var shadow = 0.0;\n\n for (var i = frame.areaLightCount; i < frame.lightCount; i = i + 1u) {\n let light = lights[i];\n\n if (light.kind == LIGHT_ENVIRONMENT || light.kind == LIGHT_RECT || (light.flags & LIGHT_CASTS_SHADOW) == 0u) {\n continue;\n }\n\n let chosen = sampleLightEntry(light, light.pdf, position, sample2D(rng));\n\n if (!chosen.valid) {\n continue;\n }\n\n let visible = luminanceOf(lightTransmittance(position, chosen, rng, NO_MATERIAL, 0.0));\n\n shadow = max(shadow, 1.0 - clamp(visible, 0.0, 1.0));\n }\n\n return shadow;\n}\n";
|
|
10
10
|
//# sourceMappingURL=lights.wgsl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lights.wgsl.d.ts","sourceRoot":"","sources":["../../src/wgsl/lights.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"lights.wgsl.d.ts","sourceRoot":"","sources":["../../src/wgsl/lights.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,6x1BAyvBvB,CAAC"}
|