use awsm_renderer_core::command::compute_pass::ComputePassDescriptor;
use crate::{
error::Result,
render::RenderContext,
render_passes::{
light_culling::{bind_group::LightCullingBindGroups, pipeline::LightCullingPipelines},
RenderPassInitContext,
},
};
pub struct LightCullingRenderPass {
pub bind_groups: LightCullingBindGroups,
pub pipelines: LightCullingPipelines,
}
impl LightCullingRenderPass {
pub async fn new(ctx: &mut RenderPassInitContext<'_>) -> Result<Self> {
let bind_groups = LightCullingBindGroups::new(ctx).await?;
let pipelines = LightCullingPipelines::new(ctx, &bind_groups).await?;
Ok(Self {
bind_groups,
pipelines,
})
}
pub fn render(&self, ctx: &RenderContext) -> Result<()> {
let buffers = ctx.light_culling_buffers;
if ctx.live_light_count() == 0 {
return Ok(());
}
let bind_group = self.bind_groups.get_bind_group()?;
let tile_pipeline = ctx
.pipelines
.compute
.get(self.pipelines.tile_pipeline_key)?;
let froxel_pipeline = ctx.pipelines.compute.get(self.pipelines.pipeline_key)?;
let tiles_x = buffers.tiles_x();
let tiles_y = buffers.tiles_y();
let compute_pass = ctx.command_encoder.begin_compute_pass(Some(
&ComputePassDescriptor::new(Some("Light Culling")).into(),
));
compute_pass.set_bind_group(0, bind_group, None)?;
compute_pass.set_pipeline(tile_pipeline);
compute_pass.dispatch_workgroups(tiles_x, Some(tiles_y), Some(1));
compute_pass.set_pipeline(froxel_pipeline);
compute_pass.dispatch_workgroups(tiles_x, Some(tiles_y), Some(buffers.slice_count));
compute_pass.end();
Ok(())
}
}