1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! Deterministic linear-sRGB environment lookups; no transport samples are drawn.
use std::sync::Arc;
use glam::Vec3;
use crate::{
optics::{
raytracer::{
DEFAULT_HEAD_SHADOW_COSINES, EnvironmentSource, LightingPreset,
environment::{
head_shadow_cosines, sample_studio_environment_with_rig_shadow_tray,
sample_studio_environment_with_rig_spec_power_shadow_tray,
},
},
studio_rig::StudioRig,
},
renderer::env_map::EnvironmentMap,
};
use super::tint::{illuminant_rgb, integrate_rgb};
enum Source<'a> {
Studio {
preset: LightingPreset,
exposure: f32,
rig: Box<StudioRig>,
shadow: [f32; 2],
rgb: Vec3,
backdrop: f32,
},
Aset {
table: Box<[Vec3; 1024]>,
rgb: Vec3,
backdrop: f32,
},
Hdr(&'a EnvironmentMap),
OwnedHdr(Arc<EnvironmentMap>),
#[cfg(test)]
Constant(Vec3),
}
/// Prepared environment radiance, expressed in linear sRGB.
pub struct WhittedEnv<'a> {
source: Source<'a>,
}
/// Borrowed prepared data: GPU uploads reuse the CPU tables and rig rather than
/// independently integrating spectra or approximating a directional environment.
#[cfg(feature = "gpu")]
pub enum WhittedGpuEnv<'a> {
Studio {
preset: LightingPreset,
exposure: f32,
rig: &'a StudioRig,
shadow: [f32; 2],
rgb: Vec3,
backdrop: f32,
},
Aset {
table: &'a [Vec3; 1024],
rgb: Vec3,
backdrop: f32,
},
Hdr(&'a EnvironmentMap),
#[cfg(test)]
Constant(Vec3),
}
impl<'a> WhittedEnv<'a> {
#[cfg(feature = "gpu")]
pub(crate) fn gpu_data(&self) -> WhittedGpuEnv<'_> {
match &self.source {
Source::Studio {
preset,
exposure,
rig,
shadow,
rgb,
backdrop,
} => WhittedGpuEnv::Studio {
preset: *preset,
exposure: *exposure,
rig,
shadow: *shadow,
rgb: *rgb,
backdrop: *backdrop,
},
Source::Aset {
table,
rgb,
backdrop,
} => WhittedGpuEnv::Aset {
table,
rgb: *rgb,
backdrop: *backdrop,
},
Source::Hdr(map) => WhittedGpuEnv::Hdr(map),
Source::OwnedHdr(map) => WhittedGpuEnv::Hdr(map),
#[cfg(test)]
Source::Constant(rgb) => WhittedGpuEnv::Constant(*rgb),
}
}
pub(super) fn new(environment: EnvironmentSource<'a>) -> Self {
let source = match environment {
EnvironmentSource::HdrMap(map) => Source::Hdr(map),
EnvironmentSource::Studio {
preset,
exposure,
light_yaw,
light_pitch,
backdrop,
head_shadow_deg,
..
} => {
let rig = StudioRig::new(light_yaw, light_pitch);
let rgb = illuminant_rgb(environment);
if preset == LightingPreset::Aset {
let table = Box::new(std::array::from_fn(|row| {
#[expect(clippy::cast_precision_loss, reason = "table index is below 1024")]
let y = (row as f32 / 1023.0).mul_add(2.0, -1.0);
// Operation order mirrors the WGSL twin 00_bindings.wgsl: the square is
// its own binding so no clippy version suggests a fused multiply-add.
let square = y * y;
let dir = Vec3::new((1.0 - square).max(0.0).sqrt(), y, 0.0);
// The lit tray (`backdrop`, 0.0 = As Lit) is part of the table, so
// the ASET surround and its windows read the same value.
integrate_rgb(|wavelength| {
sample_studio_environment_with_rig_shadow_tray(
dir,
wavelength,
preset,
exposure,
&rig,
Vec3::ZERO,
DEFAULT_HEAD_SHADOW_COSINES,
backdrop,
)
})
}));
Source::Aset {
table,
rgb,
backdrop,
}
} else {
Source::Studio {
preset,
exposure,
rig: Box::new(rig),
shadow: head_shadow_cosines(head_shadow_deg),
rgb,
backdrop,
}
}
}
};
Self { source }
}
/// Radiance reaching a leaf in `dir`, with the pixel's direction toward its eye.
#[must_use]
pub fn leaf(&self, dir: Vec3, observer: Vec3) -> Vec3 {
match &self.source {
Source::Hdr(map) => Vec3::from_array(map.radiance_rgb(dir)),
Source::OwnedHdr(map) => Vec3::from_array(map.radiance_rgb(dir)),
Source::Studio {
preset,
exposure,
rig,
shadow,
rgb,
backdrop,
} => {
// One arithmetic path for every head-shadow setting and for the lit
// backdrop tray (`backdrop` = its radiance at exposure 1, 0.0 = As Lit):
// a primary miss, a window and a reflection all call it, so they agree.
let geometry = sample_studio_environment_with_rig_spec_power_shadow_tray(
dir, *preset, *exposure, rig, observer, 1.0, *shadow, *backdrop,
);
*rgb * geometry
}
Source::Aset { table, .. } => {
let position = (dir.y.clamp(-1.0, 1.0) + 1.0) * 511.5;
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "clamped table coordinate is in 0..=1023"
)]
let lower = position as usize;
#[expect(clippy::cast_precision_loss, reason = "table index is below 1024")]
let fraction = position - lower as f32;
table[lower].lerp(table[(lower + 1).min(1023)], fraction)
}
#[cfg(test)]
Source::Constant(rgb) => *rgb,
}
}
/// Detaches a prepared environment from its scene for long-lived frame caches.
/// An HDR map is copied once; no per-tap or per-chunk copy occurs.
#[must_use]
pub fn into_owned(self) -> WhittedEnv<'static> {
self.into_owned_with_hdr(None)
}
/// Reuses the scene's existing HDR allocation when it is the exact map this
/// sampler borrows. A mismatching Arc cannot silently replace the lighting.
#[must_use]
pub fn into_owned_with_hdr(self, shared: Option<Arc<EnvironmentMap>>) -> WhittedEnv<'static> {
let source = match self.source {
Source::Hdr(map) => Source::OwnedHdr(
shared
.filter(|candidate| std::ptr::eq(map, candidate.as_ref()))
.unwrap_or_else(|| Arc::new(map.clone())),
),
Source::OwnedHdr(map) => Source::OwnedHdr(map),
Source::Studio {
preset,
exposure,
rig,
shadow,
rgb,
backdrop,
} => Source::Studio {
preset,
exposure,
rig,
shadow,
rgb,
backdrop,
},
Source::Aset {
table,
rgb,
backdrop,
} => Source::Aset {
table,
rgb,
backdrop,
},
#[cfg(test)]
Source::Constant(rgb) => Source::Constant(rgb),
};
WhittedEnv { source }
}
#[cfg(test)]
pub(super) const fn for_tests(rgb: Vec3) -> Self {
Self {
source: Source::Constant(rgb),
}
}
}