pub(crate) static PYRAMID: crate::shading::Module = crate::shading::Module {
name: "depth_pyramid",
source: include_str!("pyramid.wgsl"),
deps: &[],
};
pub(crate) const DOWNSAMPLE_DEPTH_FIRST_ENTRY: &str = "downsample_depth_first";
pub(crate) const DOWNSAMPLE_DEPTH_SECOND_ENTRY: &str = "downsample_depth_second";
pub(crate) const SUPPORTED_STORAGE_TEXTURES: u32 = 6;
const MOST_LEVELS: u32 = 12;
const FIRST_PASS_LEVELS: u32 = 6;
const FIRST_PASS_TEXELS: u32 = 64;
pub(crate) fn first_level(size: [u32; 2]) -> [u32; 2] {
size.map(|side| 1 << (31 - side.max(1).leading_zeros()))
}
fn levels(first: [u32; 2]) -> u32 {
32 - first[0].max(first[1]).leading_zeros()
}
fn complete_levels(first: [u32; 2]) -> u32 {
let levels = levels(first);
if levels <= MOST_LEVELS {
levels
} else {
FIRST_PASS_LEVELS
}
}
pub(crate) struct Pyramid {
size: [u32; 2],
all: wgpu::TextureView,
mips: Vec<wgpu::TextureView>,
complete: u32,
constants: wgpu::Buffer,
groups: Option<(wgpu::TextureView, [wgpu::BindGroup; 2])>,
}
impl Pyramid {
pub fn view(&self) -> &wgpu::TextureView {
&self.all
}
pub fn complete(&self) -> u32 {
self.complete
}
#[cfg(all(test, not(target_arch = "wasm32")))]
pub fn texture(&self) -> &wgpu::Texture {
self.all.texture()
}
}
pub(crate) struct Builder {
layouts: [wgpu::BindGroupLayout; 2],
pipelines: [wgpu::ComputePipeline; 2],
sampler: wgpu::Sampler,
dummy: wgpu::TextureView,
}
impl Builder {
pub fn new(device: &wgpu::Device) -> Self {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("depth pyramid"),
source: wgpu::ShaderSource::Wgsl(crate::shading::compose(&[&PYRAMID]).into()),
});
let storage = |binding| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::WriteOnly,
format: wgpu::TextureFormat::R32Float,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
};
let constants = wgpu::BindGroupLayoutEntry {
binding: 8,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
};
let texture = |binding, sample_type| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Texture {
sample_type,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
};
let mut first: Vec<_> = (1..=6).map(storage).collect();
first.extend([
texture(0, wgpu::TextureSampleType::Depth),
wgpu::BindGroupLayoutEntry {
binding: 7,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
count: None,
},
constants,
]);
let mut second: Vec<_> = (10..=15).map(storage).collect();
second.extend([
texture(9, wgpu::TextureSampleType::Float { filterable: false }),
constants,
]);
let layouts = [first, second].map(|entries| {
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("depth pyramid"),
entries: &entries,
})
});
let pipeline = |layout: &wgpu::BindGroupLayout, entry: &str| {
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some(entry),
layout: Some(
&device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some(entry),
bind_group_layouts: &[Some(layout)],
immediate_size: 0,
}),
),
module: &module,
entry_point: Some(entry),
compilation_options: Default::default(),
cache: None,
})
};
let pipelines = [
pipeline(&layouts[0], DOWNSAMPLE_DEPTH_FIRST_ENTRY),
pipeline(&layouts[1], DOWNSAMPLE_DEPTH_SECOND_ENTRY),
];
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("depth pyramid source"),
..Default::default()
});
let dummy = device
.create_texture(&wgpu::TextureDescriptor {
label: Some("depth pyramid stand-in level"),
size: wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R32Float,
usage: wgpu::TextureUsages::STORAGE_BINDING,
view_formats: &[],
})
.create_view(&Default::default());
Self {
layouts,
pipelines,
sampler,
dummy,
}
}
pub fn pyramid(&self, device: &wgpu::Device, size: [u32; 2]) -> Pyramid {
let first = first_level(size);
let levels = levels(first);
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("depth pyramid"),
size: wgpu::Extent3d {
width: first[0],
height: first[1],
depth_or_array_layers: 1,
},
mip_level_count: levels,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R32Float,
usage: wgpu::TextureUsages::STORAGE_BINDING
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
let mips = (0..levels.min(MOST_LEVELS))
.map(|level| {
texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("depth pyramid level"),
base_mip_level: level,
mip_level_count: Some(1),
..Default::default()
})
})
.collect();
let constants = crate::counters::buffer_init(
device,
&wgpu::util::BufferInitDescriptor {
label: Some("depth pyramid constants"),
contents: bytemuck::cast_slice(&[levels.min(MOST_LEVELS), 0, 0, 0]),
usage: wgpu::BufferUsages::UNIFORM,
},
);
Pyramid {
size,
all: texture.create_view(&Default::default()),
mips,
complete: complete_levels(first),
constants,
groups: None,
}
}
pub fn fits(pyramid: &Pyramid, size: [u32; 2]) -> bool {
pyramid.size == size
}
pub fn encode(
&self,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
pyramid: &mut Pyramid,
depth: &wgpu::TextureView,
timing: Option<&crate::timing::GpuTiming>,
) {
if pyramid
.groups
.as_ref()
.is_none_or(|(bound, _)| bound != depth)
{
let groups = self.groups(device, pyramid, depth);
pyramid.groups = Some((depth.clone(), groups));
}
let groups = &pyramid.groups.as_ref().unwrap().1;
let levels = pyramid.mips.len() as u32;
let virtual_size = pyramid.size.map(|side| (side + 1).next_power_of_two());
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("depth pyramid"),
timestamp_writes: timing.and_then(|timing| timing.compute_pass("depth pyramid")),
});
pass.set_pipeline(&self.pipelines[0]);
pass.set_bind_group(0, &groups[0], &[]);
pass.dispatch_workgroups(
virtual_size[0].div_ceil(FIRST_PASS_TEXELS),
virtual_size[1].div_ceil(FIRST_PASS_TEXELS),
1,
);
if levels > FIRST_PASS_LEVELS {
pass.set_pipeline(&self.pipelines[1]);
pass.set_bind_group(0, &groups[1], &[]);
pass.dispatch_workgroups(1, 1, 1);
}
}
fn groups(
&self,
device: &wgpu::Device,
pyramid: &Pyramid,
depth: &wgpu::TextureView,
) -> [wgpu::BindGroup; 2] {
let mip = |level: usize| pyramid.mips.get(level).unwrap_or(&self.dummy);
let view = wgpu::BindingResource::TextureView;
let mut first: Vec<_> = (0..6)
.map(|level| wgpu::BindGroupEntry {
binding: level as u32 + 1,
resource: view(mip(level)),
})
.collect();
first.extend([
wgpu::BindGroupEntry {
binding: 0,
resource: view(depth),
},
wgpu::BindGroupEntry {
binding: 7,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
wgpu::BindGroupEntry {
binding: 8,
resource: pyramid.constants.as_entire_binding(),
},
]);
let sampled = pyramid.mips.get(5).unwrap_or(&pyramid.all);
let mut second: Vec<_> = (0..6)
.map(|level| wgpu::BindGroupEntry {
binding: level as u32 + 10,
resource: view(mip(level + 6)),
})
.collect();
second.extend([
wgpu::BindGroupEntry {
binding: 9,
resource: view(sampled),
},
wgpu::BindGroupEntry {
binding: 8,
resource: pyramid.constants.as_entire_binding(),
},
]);
[(&self.layouts[0], first), (&self.layouts[1], second)].map(|(layout, entries)| {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("depth pyramid"),
layout,
entries: &entries,
})
})
}
}