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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Equirectangular skybox for the rasterizer.
//!
//! Renders an HDR environment map as the scene background, drawn as a full-screen
//! pass into the HDR film before the opaque geometry (which then overwrites it
//! wherever something is visible). The direction→UV mapping matches the path
//! tracer's HDRI lookup, so the same image produces the same sky in both
//! backends.
use crate::context::Context;
use crate::renderer::ibl::EnvironmentMap;
use crate::renderer::raytracer::environment::Environment;
use crate::resource::{multisample_state, PipelineCache};
use bytemuck::{Pod, Zeroable};
use std::path::Path;
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
struct SkyUniforms {
inv_view_proj: [[f32; 4]; 4],
// (cos(rotation), sin(rotation), intensity, unused)
params: [f32; 4],
}
/// Owns the equirectangular environment map and the full-screen pipeline used to
/// draw it as the scene background. One instance lives on each
/// [`Window`](crate::window::Window).
pub struct Skybox {
environment: Environment,
/// Mip-chained copy of the environment used as the image-based-lighting
/// source (built alongside the background environment).
ibl_env: Option<EnvironmentMap>,
rotation: f32,
intensity: f32,
/// Bumped whenever the environment image is replaced or cleared, so the path
/// tracer (which samples the same skybox) can detect the change and restart
/// accumulation. Orientation changes are tracked separately by value.
generation: u64,
layout: wgpu::BindGroupLayout,
pipeline: PipelineCache,
uniform: wgpu::Buffer,
}
impl Default for Skybox {
fn default() -> Self {
Self::new()
}
}
impl Skybox {
/// Creates a skybox with no environment set (renders nothing until one is).
pub fn new() -> Skybox {
let ctxt = Context::get();
let layout = ctxt.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("skybox_bind_group_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let pipeline_layout = ctxt.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("skybox_pipeline_layout"),
bind_group_layouts: &[Some(&layout)],
immediate_size: 0,
});
// `skybox` imports the shared equirect mapping from the `pbr_env` module.
let shader = ctxt.create_shader_module(
Some("skybox_shader"),
&crate::builtin::compile_wesl(
&[
("package::skybox", include_str!("../builtin/skybox.wgsl")),
("package::pbr_env", crate::builtin::PBR_ENV_WESL),
("package::common", crate::builtin::COMMON_WESL),
],
"package::skybox",
&[],
),
);
// Built lazily per MSAA sample count to match the HDR scene attachment.
let pipeline = PipelineCache::new(move |sample_count| {
let ctxt = Context::get();
ctxt.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("skybox_pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: Context::render_format(),
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
// No depth attachment: the sky always fills the background and is
// overwritten by the opaque pass wherever geometry is visible.
depth_stencil: None,
multisample: multisample_state(sample_count),
multiview_mask: None,
cache: None,
})
});
let uniform = ctxt.create_buffer_simple(
Some("skybox_uniform"),
std::mem::size_of::<SkyUniforms>() as u64,
wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
);
Skybox {
environment: Environment::fallback(),
ibl_env: None,
rotation: 0.0,
intensity: 1.0,
generation: 0,
layout,
pipeline,
uniform,
}
}
/// Whether an environment map is currently set (otherwise rendering is a no-op).
pub fn is_set(&self) -> bool {
self.environment.present
}
/// Sets the skybox from an equirectangular image file (HDR `.hdr`, EXR, or any
/// format the `image` crate decodes). Returns `false` if it can't be decoded.
pub fn set_from_file(&mut self, path: &Path) -> bool {
match image::open(path) {
Ok(img) => {
self.set_image(&img);
true
}
Err(_) => false,
}
}
/// Sets the skybox from an already-decoded equirectangular image.
pub fn set_image(&mut self, image: &image::DynamicImage) {
self.environment = Environment::from_image(image);
self.ibl_env = Some(EnvironmentMap::from_image(image));
self.generation += 1;
}
/// Clears the skybox (subsequent frames render no background or IBL).
pub fn clear(&mut self) {
self.environment = Environment::fallback();
self.ibl_env = None;
self.generation += 1;
}
/// The GPU-resident equirectangular environment, shared with the path tracer
/// so a skybox set on the window also lights the ray-traced view.
pub(crate) fn environment(&self) -> &Environment {
&self.environment
}
/// A counter that changes whenever the environment image is replaced or
/// cleared; the path tracer uses it to restart accumulation on a new skybox.
pub(crate) fn generation(&self) -> u64 {
self.generation
}
/// The mip-chained environment map used for image-based lighting, if set.
pub fn ibl_env(&self) -> Option<&EnvironmentMap> {
self.ibl_env.as_ref()
}
/// The environment Y-rotation in radians.
pub fn rotation(&self) -> f32 {
self.rotation
}
/// The environment luminance multiplier.
pub fn intensity(&self) -> f32 {
self.intensity
}
/// Sets the skybox Y-axis rotation (radians) and a luminance multiplier.
pub fn set_orientation(&mut self, rotation_radians: f32, intensity: f32) {
self.rotation = rotation_radians;
self.intensity = intensity.max(0.0);
}
/// Draws the skybox into `color_view` (the HDR scene attachment) using the
/// camera's inverse view-projection. A no-op when no environment is set.
pub(crate) fn render(
&self,
encoder: &mut wgpu::CommandEncoder,
color_view: &wgpu::TextureView,
sample_count: u32,
inverse_view_proj: glamx::Mat4,
gpu: Option<&mut crate::renderer::timings::GpuTimer>,
) {
if !self.environment.present {
return;
}
let ctxt = Context::get();
ctxt.write_buffer(
&self.uniform,
0,
bytemuck::bytes_of(&SkyUniforms {
inv_view_proj: inverse_view_proj.to_cols_array_2d(),
params: [
self.rotation.cos(),
self.rotation.sin(),
self.intensity,
0.0,
],
}),
);
let bind_group = ctxt.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("skybox_bind_group"),
layout: &self.layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.uniform.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&self.environment.view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&self.environment.sampler),
},
],
});
let pipeline = self.pipeline.get(sample_count);
let skybox_ts = gpu.and_then(|g| g.render_scope("skybox"));
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("skybox_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: color_view,
resolve_target: None,
ops: wgpu::Operations {
// Load: the clear pass already ran; the sky simply overwrites
// the cleared background everywhere it draws.
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: skybox_ts,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, &bind_group, &[]);
pass.draw(0..3, 0..1);
}
}