use crcbl_hal::{
BindGroupEntry, BindGroupHandle, BindGroupLayoutDesc, BindGroupLayoutEntry,
BindGroupLayoutHandle, BindingFlags, BindingKind, BindingResource, BufferDesc, BufferHandle,
BufferUsage, ClearValue, ColorTargetState, Device, Format, GraphicsPipelineHandle, HalError,
ImageViewHandle, ImageViewType, LoadOp, MemoryLocation, PipelineLayoutDesc,
PipelineLayoutHandle, SampleType, ShaderStages, StoreOp, check_portable_storage_buffers,
};
use crcbl_shaders::{CONTACT_SHADOWS, contact_shadows};
use crate::graph::{ImageId, RenderGraph};
use crate::ssao::cached_group;
const FULLSCREEN_VERTICES: u32 = 3;
const LABEL: &str = "contact-shadows";
#[derive(Debug)]
pub(crate) struct ContactShadows {
uniforms: Vec<BufferHandle>,
layout: BindGroupLayoutHandle,
pipeline_layout: PipelineLayoutHandle,
pipeline: GraphicsPipelineHandle,
groups: Vec<Option<(Vec<ImageViewHandle>, BindGroupHandle)>>,
}
impl ContactShadows {
pub(crate) const PASSES: u32 = 1;
pub(crate) fn new(
device: &dyn Device,
frames: usize,
build_fullscreen: impl Fn(
&dyn Device,
&str,
&crcbl_shaders::Shader,
PipelineLayoutHandle,
&[ColorTargetState],
) -> Result<GraphicsPipelineHandle, HalError>,
) -> Result<Self, HalError> {
let entries = [
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
kind: BindingKind::UniformBuffer { dynamic: false },
count: 1,
flags: BindingFlags::empty(),
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
kind: BindingKind::SampledImage {
view_type: ImageViewType::D2,
sample_type: SampleType::Depth,
},
count: 1,
flags: BindingFlags::empty(),
},
];
let desc = BindGroupLayoutDesc {
label: Some("contact shadows depth"),
entries: &entries,
};
check_portable_storage_buffers(Some("contact shadows"), &[&desc])?;
let layout = device.create_bind_group_layout(&desc)?;
let set_layouts = [layout];
let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDesc {
label: Some("contact shadows"),
bind_group_layouts: &set_layouts,
push_constants: None,
})?;
let targets = [ColorTargetState::opaque(Format::R8Unorm)];
let pipeline = build_fullscreen(
device,
"contact shadows",
&CONTACT_SHADOWS,
pipeline_layout,
&targets,
)?;
let mut uniforms = Vec::with_capacity(frames);
for _ in 0..frames {
uniforms.push(device.create_buffer(&BufferDesc {
label: Some("contact shadow params"),
size: contact_shadows::PARAMS_SIZE as u64,
usage: BufferUsage::UNIFORM,
memory: MemoryLocation::HostUpload,
})?);
}
Ok(Self {
uniforms,
layout,
pipeline_layout,
pipeline,
groups: vec![None; frames],
})
}
pub(crate) fn begin_frame(
&self,
device: &dyn Device,
frame: usize,
params: contact_shadows::ContactShadowParams,
) -> Result<(), HalError> {
device.write_buffer(self.uniforms[frame], 0, ¶ms.to_bytes())
}
pub(crate) fn add_passes<'a>(
&'a mut self,
graph: &mut RenderGraph<'a>,
frame: usize,
depth: ImageId,
mask: ImageId,
) -> ImageId {
let pipeline = self.pipeline;
let pipeline_layout = self.pipeline_layout;
let layout = self.layout;
let uniforms = self.uniforms[frame];
let cached = &mut self.groups[frame];
graph
.add_render_pass(LABEL)
.color(
mask,
LoadOp::DontCare,
StoreOp::Store,
ClearValue::default(),
)
.read_image(depth)
.execute(move |ctx| {
let view = ctx.image_view(depth);
let device = ctx.device();
let entries = [
BindGroupEntry {
binding: 0,
array_index: 0,
resource: BindingResource::whole_buffer(uniforms),
},
BindGroupEntry {
binding: 1,
array_index: 0,
resource: BindingResource::ImageView(view),
},
];
let Some(group) = cached_group(
cached,
device,
&[(1, view)],
"contact shadows depth",
layout,
&entries,
) else {
return;
};
let encoder = ctx.encoder();
encoder.bind_graphics_pipeline(pipeline);
encoder.bind_group(0, group, &[], pipeline_layout);
encoder.draw(0..FULLSCREEN_VERTICES, 0..1);
});
mask
}
pub(crate) fn destroy(self, device: &dyn Device) {
for cached in self.groups.into_iter().flatten() {
device.destroy_bind_group(cached.1);
}
device.destroy_graphics_pipeline(self.pipeline);
device.destroy_pipeline_layout(self.pipeline_layout);
device.destroy_bind_group_layout(self.layout);
for buffer in self.uniforms {
device.destroy_buffer(buffer);
}
}
}