use crate::context::Context;
pub const MAX_PROBES: usize = 8;
pub const PROBE_WIDTH: u32 = 256;
pub const PROBE_HEIGHT: u32 = 128;
#[derive(Copy, Clone, Debug)]
pub struct ReflectionProbe {
pub center: glamx::Vec3,
pub half_extents: glamx::Vec3,
pub falloff: f32,
pub intensity: f32,
pub rotation: f32,
}
impl Default for ReflectionProbe {
fn default() -> Self {
ReflectionProbe {
center: glamx::Vec3::ZERO,
half_extents: glamx::Vec3::splat(5.0),
falloff: 0.5,
intensity: 1.0,
rotation: 0.0,
}
}
}
impl ReflectionProbe {
pub fn new(center: glamx::Vec3, half_extent: f32) -> Self {
ReflectionProbe {
center,
half_extents: glamx::Vec3::splat(half_extent),
..Default::default()
}
}
}
pub struct ReflectionProbes {
texture: wgpu::Texture,
array_view: wgpu::TextureView,
sampler: wgpu::Sampler,
mip_count: u32,
probes: Vec<ReflectionProbe>,
}
impl ReflectionProbes {
pub fn new() -> ReflectionProbes {
let ctxt = Context::get();
let mip_count = (32 - PROBE_WIDTH.max(PROBE_HEIGHT).leading_zeros()).max(1);
let texture = ctxt.create_texture(&wgpu::TextureDescriptor {
label: Some("reflection_probe_array"),
size: wgpu::Extent3d {
width: PROBE_WIDTH,
height: PROBE_HEIGHT,
depth_or_array_layers: MAX_PROBES as u32,
},
mip_level_count: mip_count,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba16Float,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
});
let array_view = texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("reflection_probe_array_view"),
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
let sampler = ctxt.create_sampler(&wgpu::SamplerDescriptor {
label: Some("reflection_probe_sampler"),
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::MipmapFilterMode::Linear,
..Default::default()
});
ReflectionProbes {
texture,
array_view,
sampler,
mip_count,
probes: Vec::new(),
}
}
pub fn len(&self) -> usize {
self.probes.len()
}
pub fn is_empty(&self) -> bool {
self.probes.is_empty()
}
pub fn array_view(&self) -> &wgpu::TextureView {
&self.array_view
}
pub fn sampler(&self) -> &wgpu::Sampler {
&self.sampler
}
pub fn max_lod(&self) -> f32 {
(self.mip_count.max(1) - 1) as f32
}
pub fn probes(&self) -> &[ReflectionProbe] {
&self.probes
}
pub fn probe_mut(&mut self, idx: usize) -> Option<&mut ReflectionProbe> {
self.probes.get_mut(idx)
}
pub fn texture(&self) -> &wgpu::Texture {
&self.texture
}
pub fn mip_count(&self) -> u32 {
self.mip_count
}
pub fn layer_mip0_view(&self, idx: usize) -> wgpu::TextureView {
self.texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("reflection_probe_layer_mip0"),
dimension: Some(wgpu::TextureViewDimension::D2),
base_mip_level: 0,
mip_level_count: Some(1),
base_array_layer: idx as u32,
array_layer_count: Some(1),
..Default::default()
})
}
pub fn add(&mut self, probe: ReflectionProbe) -> Option<usize> {
if self.probes.len() >= MAX_PROBES {
return None;
}
let idx = self.probes.len();
self.probes.push(probe);
Some(idx)
}
pub fn set_image(&mut self, idx: usize, img: &image::DynamicImage) {
if idx >= self.probes.len() {
return;
}
let resized = img.resize_exact(
PROBE_WIDTH,
PROBE_HEIGHT,
image::imageops::FilterType::Triangle,
);
let rgba = resized.to_rgba32f();
let halves: Vec<u16> = rgba.as_raw().iter().map(|&v| f32_to_f16(v)).collect();
let ctxt = Context::get();
ctxt.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: 0,
y: 0,
z: idx as u32,
},
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(&halves),
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(PROBE_WIDTH * 8),
rows_per_image: Some(PROBE_HEIGHT),
},
wgpu::Extent3d {
width: PROBE_WIDTH,
height: PROBE_HEIGHT,
depth_or_array_layers: 1,
},
);
let mut encoder = ctxt.create_command_encoder(Some("reflection_probe_mipgen"));
self.generate_layer_mips(&mut encoder, idx, None);
ctxt.submit(std::iter::once(encoder.finish()));
}
pub(crate) fn generate_layer_mips(
&self,
encoder: &mut wgpu::CommandEncoder,
layer: usize,
mut gpu: Option<&mut crate::renderer::timings::GpuTimer>,
) {
if self.mip_count <= 1 || layer >= MAX_PROBES {
return;
}
let ctxt = Context::get();
let pipeline = Self::downsample_pipeline();
let layout = pipeline.get_bind_group_layout(0);
for mip in 1..self.mip_count {
let src_view = self.texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("reflection_probe_mip_src"),
dimension: Some(wgpu::TextureViewDimension::D2),
base_mip_level: mip - 1,
mip_level_count: Some(1),
base_array_layer: layer as u32,
array_layer_count: Some(1),
..Default::default()
});
let dst_view = self.texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("reflection_probe_mip_dst"),
dimension: Some(wgpu::TextureViewDimension::D2),
base_mip_level: mip,
mip_level_count: Some(1),
base_array_layer: layer as u32,
array_layer_count: Some(1),
..Default::default()
});
let bind_group = ctxt.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("reflection_probe_downsample_bg"),
layout: &layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&src_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
});
let mip_ts = gpu.as_deref_mut().and_then(|g| g.render_scope("probe"));
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("reflection_probe_downsample_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &dst_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: mip_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);
}
}
fn downsample_pipeline() -> wgpu::RenderPipeline {
let ctxt = Context::get();
let shader = ctxt.create_shader_module(
Some("reflection_probe_downsample"),
&crate::builtin::compile_shader_with_common(
"package::env_downsample",
crate::builtin::ENV_DOWNSAMPLE_WESL,
),
);
let layout = ctxt.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("reflection_probe_downsample_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
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: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let pipeline_layout = ctxt.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("reflection_probe_downsample_pipeline_layout"),
bind_group_layouts: &[Some(&layout)],
immediate_size: 0,
});
ctxt.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("reflection_probe_downsample_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: wgpu::TextureFormat::Rgba16Float,
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,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview_mask: None,
cache: None,
})
}
}
impl Default for ReflectionProbes {
fn default() -> Self {
Self::new()
}
}
const FACE_FORWARD: [[f32; 3]; 6] = [
[1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, -1.0],
];
const FACE_UP: [[f32; 3]; 6] = [
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
];
pub struct CubeFaceCamera {
eye: glamx::Vec3,
view: glamx::Pose3,
proj: glamx::Mat4,
znear: f32,
zfar: f32,
}
impl CubeFaceCamera {
pub fn new(eye: glamx::Vec3, face: usize, znear: f32, zfar: f32) -> CubeFaceCamera {
let f = FACE_FORWARD[face];
let u = FACE_UP[face];
let fwd = glamx::Vec3::new(f[0], f[1], f[2]);
let up = glamx::Vec3::new(u[0], u[1], u[2]);
let proj = glamx::glam::camera::rh::proj::opengl::perspective(
core::f32::consts::FRAC_PI_2,
1.0,
znear,
zfar,
);
let view = glamx::Pose3::look_at_rh(eye, eye + fwd, up);
CubeFaceCamera {
eye,
view,
proj,
znear,
zfar,
}
}
}
impl crate::camera::Camera3d for CubeFaceCamera {
fn handle_event(&mut self, _: &crate::window::Canvas, _: &crate::event::WindowEvent) {}
fn update(&mut self, _: &crate::window::Canvas) {}
fn eye(&self) -> glamx::Vec3 {
self.eye
}
fn view_transform(&self) -> glamx::Pose3 {
self.view
}
fn transformation(&self) -> glamx::Mat4 {
self.proj * self.view.to_mat4()
}
fn inverse_transformation(&self) -> glamx::Mat4 {
self.transformation().inverse()
}
fn clip_planes(&self) -> (f32, f32) {
(self.znear, self.zfar)
}
fn view_transform_pair(&self, _pass: usize) -> (glamx::Pose3, glamx::Mat4) {
(self.view, self.proj)
}
}
pub struct ProbeCapture {
size: u32,
_color: wgpu::Texture,
face_views: Vec<wgpu::TextureView>,
array_view: wgpu::TextureView,
_depth: wgpu::Texture,
depth_view: wgpu::TextureView,
sampler: wgpu::Sampler,
reproject_pipeline: wgpu::RenderPipeline,
reproject_layout: wgpu::BindGroupLayout,
}
impl ProbeCapture {
pub fn new(size: u32) -> ProbeCapture {
let ctxt = Context::get();
let color = ctxt.create_texture(&wgpu::TextureDescriptor {
label: Some("probe_capture_color"),
size: wgpu::Extent3d {
width: size,
height: size,
depth_or_array_layers: 6,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba16Float,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let face_views: Vec<_> = (0..6)
.map(|i| {
color.create_view(&wgpu::TextureViewDescriptor {
label: Some("probe_capture_face"),
dimension: Some(wgpu::TextureViewDimension::D2),
base_array_layer: i,
array_layer_count: Some(1),
..Default::default()
})
})
.collect();
let array_view = color.create_view(&wgpu::TextureViewDescriptor {
label: Some("probe_capture_array"),
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
let depth = ctxt.create_texture(&wgpu::TextureDescriptor {
label: Some("probe_capture_depth"),
size: wgpu::Extent3d {
width: size,
height: size,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Context::depth_format(),
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
});
let depth_view = depth.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = ctxt.create_sampler(&wgpu::SamplerDescriptor {
label: Some("probe_capture_sampler"),
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
});
let shader = ctxt.create_shader_module(
Some("cube_to_equirect"),
&crate::builtin::compile_shader_with_common(
"package::cube_to_equirect",
include_str!("../builtin/cube_to_equirect.wgsl"),
),
);
let reproject_layout = ctxt.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("probe_reproject_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2Array,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let pl = ctxt.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("probe_reproject_pipeline_layout"),
bind_group_layouts: &[Some(&reproject_layout)],
immediate_size: 0,
});
let reproject_pipeline = ctxt.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("probe_reproject_pipeline"),
layout: Some(&pl),
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: wgpu::TextureFormat::Rgba16Float,
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,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview_mask: None,
cache: None,
});
ProbeCapture {
size,
_color: color,
face_views,
array_view,
_depth: depth,
depth_view,
sampler,
reproject_pipeline,
reproject_layout,
}
}
pub fn size(&self) -> u32 {
self.size
}
pub fn face_color_view(&self, face: usize) -> &wgpu::TextureView {
&self.face_views[face]
}
pub fn depth_view(&self) -> &wgpu::TextureView {
&self.depth_view
}
pub(crate) fn reproject(
&self,
encoder: &mut wgpu::CommandEncoder,
dst: &wgpu::TextureView,
gpu: &mut crate::renderer::timings::GpuTimer,
) {
let ctxt = Context::get();
let bg = ctxt.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("probe_reproject_bg"),
layout: &self.reproject_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&self.array_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
});
let reproject_ts = gpu.render_scope("probe");
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("probe_reproject_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: dst,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: reproject_ts,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(&self.reproject_pipeline);
pass.set_bind_group(0, &bg, &[]);
pass.draw(0..3, 0..1);
}
}
fn f32_to_f16(value: f32) -> u16 {
let bits = value.to_bits();
let sign = ((bits >> 16) & 0x8000) as u16;
let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
let mant = (bits >> 13) & 0x3ff;
if exp <= 0 {
sign
} else if exp >= 0x1f {
sign | 0x7c00
} else {
sign | ((exp as u16) << 10) | (mant as u16)
}
}