use super::{
gpu::Gpu,
materials::{Draw, MaterialFrame},
pipeline::PipelineSet,
textures::TextureStore,
viewport::scissor,
};
use crate::DrawData;
use winit::dpi::PhysicalSize;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Area {
pub origin: [u32; 2],
pub size: [u32; 2],
}
impl Area {
pub fn whole(size: PhysicalSize<u32>) -> Self {
Self {
origin: [0, 0],
size: [size.width, size.height],
}
}
pub fn scissor(&self, clip: crate::Rect, scale: f32) -> Option<[u32; 4]> {
let local = PhysicalSize::new(self.size[0], self.size[1]);
scissor(clip, scale, local)
.map(|[x, y, w, h]| [self.origin[0] + x, self.origin[1] + y, w, h])
}
}
#[derive(Debug, Default)]
pub(super) struct Degraded {
pub backdrop: Vec<usize>,
pub materials: Vec<usize>,
}
impl Gpu {
pub(super) fn record_commands(
&self,
pass: &mut wgpu::RenderPass<'_>,
data: &DrawData,
store: &TextureStore,
materials: &MaterialFrame,
set: &PipelineSet,
area: Area,
mut degraded: Option<&mut Degraded>,
) -> u64 {
if data.indices.is_empty() {
return 0;
}
pass.set_pipeline(&set.plain);
pass.set_bind_group(0, &self.viewport_group, &[]);
pass.set_vertex_buffer(0, self.vertices.slice(..));
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
let mut draws = 0;
for (index, command) in data.commands.iter().enumerate() {
if command.indices.is_empty() {
continue;
}
let Some([x, y, width, height]) = area.scissor(command.clip_rect, data.scale_factor)
else {
continue;
};
draws += 1;
if let Some(degraded) = degraded.as_deref_mut() {
if command.blur.is_some() {
if command.material.is_none() {
degraded.backdrop.push(index);
continue;
}
degraded.materials.push(index);
}
}
pass.set_scissor_rect(x, y, width, height);
match materials.draw(index) {
Draw::Skip => continue,
Draw::Material { pipeline, offset } => {
pass.set_pipeline(pipeline);
materials.bind(pass, *offset, None);
}
Draw::Plain => pass.set_pipeline(set.for_command(command, data)),
}
pass.set_bind_group(1, &store.textures[&command.texture].bind_group, &[]);
pass.draw_indexed(command.indices.clone(), 0, 0..1);
}
draws
}
}