1use std::any::Any;
22use std::ops::Range;
23use std::rc::Rc;
24
25use mln_gfx::Size;
26use mln_gfx::depth_mode::DepthMode;
27use mln_gfx::gpu::Gpu;
28use mln_gfx::shaders::bindings::{BUFFER_IDS, PAINT_PARAMS_ID, PROJECTION_ID};
29use mln_gfx::shaders::block::{Block, DrawBlocks, UniformBlock};
30use mln_gfx::shaders::layer_ubo::{GlobalPaintParamsUBO, ProjectionUBO};
31use mln_gfx::shaders::program::{BufferKind, Program, Projection};
32use mln_gfx::stencil_mode::StencilMode;
33use mln_gfx::types::DepthMaskType;
34
35use crate::camera::{FrameProjection, View};
36use crate::draw::after_gpu::AfterGpu;
37use crate::draw::context::Context;
38use crate::draw::identity::SimpleIdentity;
39use crate::draw::objects::{DrawBuffers, Mesh};
40use crate::draw::target::Target;
41use crate::render::layer_composite::Composite;
42use crate::render::light::EvaluatedLight;
43use crate::render::paint_parameters::{Composited, PaintParameters, StencilClear, TransformParameters};
44use crate::render::pass::RenderPass;
45use crate::render::terrain::FrameGround;
46use crate::tile::{OverscaledTileID, UnwrappedTileID};
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub(crate) enum DrawsInto {
51 Map,
53 Drape { lit: bool },
56}
57
58#[derive(Clone)]
61pub struct LayerFrame<'a> {
62 pub view: &'a View,
63 pub(crate) transform_params: &'a TransformParameters,
64 pub camera_moving: bool,
67 pub light: &'a EvaluatedLight,
68 pub pixel_ratio: f32,
69 pub renderable_size: Size,
70 pub pixels_to_gl_units: [f32; 2],
71 pub terrain: bool,
74 pub composites_with_frame_depth: bool,
78 pub ground: Option<Rc<FrameGround>>,
81 pub(crate) into: DrawsInto,
82}
83
84impl LayerFrame<'_> {
85 #[must_use]
87 pub fn view(&self) -> &View {
88 self.view
89 }
90
91 #[must_use]
93 pub fn pixel_ratio(&self) -> f32 {
94 self.pixel_ratio
95 }
96
97 #[must_use]
99 pub fn size(&self) -> Size {
100 self.renderable_size
101 }
102
103 #[must_use]
106 pub fn drape_lit(&self) -> bool {
107 matches!(self.into, DrawsInto::Drape { lit: true })
108 }
109}
110
111#[derive(Clone, Copy, Debug, PartialEq, Eq)]
113pub enum Depth {
114 Off,
116 Layer { sub_layer: u8, mask: DepthMaskType },
119 Volume { mask: DepthMaskType },
122}
123
124#[derive(Clone, Copy, Debug, PartialEq, Eq)]
127pub struct Draw {
128 pub order: i64,
133 pub opaque: bool,
137 pub depth: Depth,
138 pub clip: bool,
143}
144
145impl Draw {
146 pub const DEFAULT: Draw = Draw {
149 order: 0,
150 opaque: false,
151 depth: Depth::Layer { sub_layer: 0, mask: DepthMaskType::ReadOnly },
152 clip: false,
153 };
154
155 fn passes(self) -> RenderPass {
157 if self.opaque { RenderPass::OPAQUE | RenderPass::TRANSLUCENT } else { RenderPass::TRANSLUCENT }
158 }
159}
160
161impl Default for Draw {
162 fn default() -> Self {
163 Draw::DEFAULT
164 }
165}
166
167struct GroupDraw {
170 mesh: Mesh,
171 tile: Option<OverscaledTileID>,
173 pass: RenderPass,
174 depth: Depth,
175 clip: bool,
176}
177
178struct ProgramBlocks {
181 program: &'static Program,
182 at: [Option<(usize, &'static Block)>; BUFFER_IDS],
183 drawn: bool,
185}
186
187impl ProgramBlocks {
188 fn of(program: &'static Program) -> ProgramBlocks {
189 let mut at = [None; BUFFER_IDS];
190 let mut offset = 0;
191 for buffer in program.buffers {
192 if let BufferKind::Storage { block, .. } | BufferKind::DrawUniform(block) = buffer.kind {
193 at[buffer.id] = Some((offset, block));
194 offset += block.size;
195 }
196 }
197 ProgramBlocks { program, at, drawn: false }
198 }
199}
200
201struct Pushed {
204 program: usize,
205 projection: ProjectionUBO,
206 blocks: usize,
207}
208
209#[derive(Default)]
213struct Pushes {
214 pushed: Vec<Pushed>,
215 bytes: Vec<u8>,
217 programs: Vec<ProgramBlocks>,
219 shared: Vec<(&'static Block, Range<usize>)>,
222 shared_bytes: Vec<u8>,
223 scratch: Vec<u8>,
225}
226
227fn same_block(a: &Block, b: &Block) -> bool {
229 std::ptr::eq(a, b) || a.name == b.name
230}
231
232impl Pushes {
233 fn clear(&mut self) {
234 self.pushed.clear();
235 self.bytes.clear();
236 self.shared.clear();
237 self.shared_bytes.clear();
238 }
239
240 fn push(
243 &mut self,
244 program: &'static Program,
245 projection: Option<&ProjectionUBO>,
246 start: usize,
247 layouts: &'static [&'static Block],
248 ) {
249 debug_assert!(
250 program
251 .buffers
252 .iter()
253 .filter_map(|buffer| match buffer.kind {
254 BufferKind::Storage { block, .. } | BufferKind::DrawUniform(block) => Some(block),
255 _ => None,
256 })
257 .map(|block| block.name)
258 .eq(layouts.iter().map(|block| block.name)),
259 "a draw of {} pushed with blocks other than its program's of each draw",
260 program.name
261 );
262 debug_assert!(program.projection != Projection::Own, "a draw of {} with a projection of its own", program.name);
263 let index = self.programs.iter().position(|of| std::ptr::eq(of.program, program)).unwrap_or_else(|| {
264 self.programs.push(ProgramBlocks::of(program));
265 self.programs.len() - 1
266 });
267 self.pushed.push(Pushed { program: index, projection: projection.copied().unwrap_or_default(), blocks: start });
268 }
269
270 fn block(&mut self, layout: &'static Block, bytes: &[u8]) {
272 if let Some((_, range)) = self.shared.iter().find(|(given, _)| same_block(given, layout)) {
273 self.shared_bytes[range.clone()].copy_from_slice(bytes);
274 } else {
275 let start = self.shared_bytes.len();
276 self.shared_bytes.extend_from_slice(bytes);
277 self.shared.push((layout, start..self.shared_bytes.len()));
278 }
279 }
280
281 fn write(
291 &mut self,
292 gpu: &mut dyn Gpu,
293 context: &mut Context,
294 order: &[(i64, usize)],
295 paint_params: Option<&GlobalPaintParamsUBO>,
296 buffers: &mut DrawBuffers,
297 ) {
298 let Pushes { pushed, bytes, programs, shared, shared_bytes, scratch } = self;
299 for of in programs.iter_mut() {
300 of.drawn = false;
301 }
302 for &(_, index) in order {
303 programs[pushed[index].program].drawn = true;
304 }
305 let mut written = [false; BUFFER_IDS];
306 let (mut per_draw_projection, mut reads_paint_params) = (false, false);
307 for of in programs.iter().filter(|of| of.drawn) {
308 per_draw_projection |= of.program.projection == Projection::PerDraw;
309 reads_paint_params |= of.program.paint_params.is_some();
310 }
311 let shared_first = programs.iter().filter(|of| of.drawn).flat_map(|of| {
316 let shared = of.program.buffers.iter().filter(|buffer| matches!(buffer.kind, BufferKind::Uniform(_)));
317 shared.map(move |buffer| (of, buffer))
318 });
319 let per_draw = programs.iter().filter(|of| of.drawn).flat_map(|of| {
320 let per_draw = of.program.buffers.iter().filter(|buffer| !matches!(buffer.kind, BufferKind::Uniform(_)));
321 per_draw.map(move |buffer| (of, buffer))
322 });
323 for (of, buffer) in shared_first.chain(per_draw) {
324 if matches!(buffer.kind, BufferKind::Vertices(_)) || std::mem::replace(&mut written[buffer.id], true) {
326 continue;
327 }
328 match buffer.kind {
329 BufferKind::Uniform(block) => {
330 let Some((_, range)) = shared.iter().find(|(given, _)| same_block(given, block)) else {
331 debug_assert!(false, "draws of {} without their uniform block {}", of.program.name, block.name);
332 continue;
333 };
334 buffers.write_uniform(gpu, buffer.id, &shared_bytes[range.clone()], context.device());
335 }
336 BufferKind::Storage { stride, .. } => {
337 scratch.clear();
338 for &(_, index) in order {
339 let Pushed { program, blocks, .. } = pushed[index];
340 let end = scratch.len() + stride;
341 if let Some((offset, block)) = programs[program].at[buffer.id] {
342 let at = blocks + offset;
343 scratch.extend_from_slice(&bytes[at..at + block.size]);
344 }
345 scratch.resize(end, 0);
346 }
347 buffers.write_storage(gpu, buffer.id, scratch, context.device());
348 }
349 BufferKind::DrawUniform(block) => {
350 let mut own = order.iter().map(|&(_, index)| &pushed[index]).filter(|draw| {
351 programs[draw.program].at[buffer.id].is_some_and(|(_, of)| same_block(of, block))
352 });
353 let Some(&Pushed { program, blocks, .. }) = own.next() else { continue };
354 debug_assert!(own.next().is_none(), "two draws of a block of {}'s own", block.name);
355 if let Some((offset, _)) = programs[program].at[buffer.id] {
356 let at = blocks + offset;
357 buffers.write_uniform(gpu, buffer.id, &bytes[at..at + block.size], context.device());
358 }
359 }
360 BufferKind::Vertices(_) => {}
361 }
362 }
363 if per_draw_projection {
364 scratch.clear();
365 for &(_, index) in order {
366 scratch.extend_from_slice(bytemuck::bytes_of(&pushed[index].projection));
367 }
368 buffers.write_storage(gpu, PROJECTION_ID, scratch, context.device());
369 } else if buffers.get(PROJECTION_ID).is_none() {
370 let none = ProjectionUBO::default();
373 buffers.write_storage(gpu, PROJECTION_ID, bytemuck::bytes_of(&none), context.device());
374 }
375 if let Some(paint_params) = paint_params.filter(|_| reads_paint_params) {
376 buffers.write_pod(gpu, PAINT_PARAMS_ID, paint_params, context.device());
377 }
378 }
379}
380
381pub struct Group {
385 id: SimpleIdentity,
387 draws: Vec<GroupDraw>,
390 buffers: DrawBuffers,
393 composite: Option<Composite>,
397 stencil_tiles: Vec<UnwrappedTileID>,
400 pending: Vec<Option<GroupDraw>>,
403 order: Vec<(i64, usize)>,
405 pushes: Pushes,
406}
407
408impl Default for Group {
409 fn default() -> Self {
411 Group {
412 id: SimpleIdentity::new(),
413 draws: Vec::new(),
414 buffers: DrawBuffers::default(),
415 composite: None,
416 stencil_tiles: Vec::new(),
417 pending: Vec::new(),
418 order: Vec::new(),
419 pushes: Pushes::default(),
420 }
421 }
422}
423
424impl Group {
425 pub fn push<B: DrawBlocks>(&mut self, mesh: &Mesh, draw: Draw, blocks: B) -> &mut Self {
430 self.add(mesh, None, None, draw, blocks)
431 }
432
433 pub fn push_at<B: DrawBlocks>(
438 &mut self,
439 mesh: &Mesh,
440 tile: OverscaledTileID,
441 projection: &ProjectionUBO,
442 draw: Draw,
443 blocks: B,
444 ) -> &mut Self {
445 self.add(mesh, Some(tile), Some(projection), draw, blocks)
446 }
447
448 fn add<B: DrawBlocks>(
449 &mut self,
450 mesh: &Mesh,
451 tile: Option<OverscaledTileID>,
452 projection: Option<&ProjectionUBO>,
453 draw: Draw,
454 blocks: B,
455 ) -> &mut Self {
456 debug_assert!(
457 !draw.clip || tile.is_some() || matches!(draw.depth, Depth::Volume { .. }),
458 "a draw that clips without a tile"
459 );
460 let start = self.pushes.bytes.len();
461 blocks.each_bytes(|block| self.pushes.bytes.extend_from_slice(block));
462 self.pushes.push(mesh.program(), projection, start, B::BLOCKS);
463 self.order.push((draw.order, self.pending.len()));
464 self.pending.push(Some(GroupDraw {
465 mesh: mesh.clone(),
466 tile,
467 pass: draw.passes(),
468 depth: draw.depth,
469 clip: draw.clip,
470 }));
471 self
472 }
473
474 pub fn block<T: UniformBlock>(&mut self, block: &T) -> &mut Self {
478 self.pushes.block(T::BLOCK, bytemuck::bytes_of(block));
479 self
480 }
481
482 pub fn composite(&mut self, composite: Option<Composite>) -> &mut Self {
485 self.composite = composite;
486 self
487 }
488
489 pub(crate) fn clear(&mut self) {
492 self.draws.clear();
493 self.clear_pushed();
494 self.buffers.sweep();
495 }
496
497 fn clear_pushed(&mut self) {
498 self.pending.clear();
499 self.order.clear();
500 self.pushes.clear();
501 }
502
503 pub(crate) fn finish(
509 &mut self,
510 gpu: &mut dyn Gpu,
511 context: &mut Context,
512 paint_params: Option<&GlobalPaintParamsUBO>,
513 tiles: impl IntoIterator<Item = UnwrappedTileID>,
514 ) {
515 self.order.sort_unstable();
516 if !self.order.is_empty() {
517 self.pushes.write(gpu, context, &self.order, paint_params, &mut self.buffers);
518 }
519 let Group { draws, pending, order, stencil_tiles, .. } = self;
520 draws.clear();
521 draws.extend(order.iter().filter_map(|&(_, index)| pending[index].take()));
522 stencil_tiles.clear();
523 if draws.iter().any(|draw| draw.clip) {
524 stencil_tiles.extend(tiles);
525 }
526 self.buffers.sweep();
527 self.clear_pushed();
528 }
529
530 #[must_use]
533 pub(crate) fn stencil_tiles(&self) -> Option<&[UnwrappedTileID]> {
534 Some(&self.stencil_tiles[..]).filter(|tiles| !tiles.is_empty())
535 }
536
537 #[must_use]
539 pub(crate) fn draws(&self) -> bool {
540 !self.draws.is_empty()
541 }
542
543 #[must_use]
546 pub(crate) fn composited(&self) -> Option<(SimpleIdentity, Composite)> {
547 self.composite.filter(|_| !self.draws.is_empty()).map(|composite| (self.id, composite))
548 }
549
550 #[cfg(test)]
552 pub(crate) fn meshes(&self) -> impl Iterator<Item = &Mesh> {
553 self.draws.iter().map(|draw| &draw.mesh)
554 }
555
556 pub(crate) fn upload(&self, gpu: &mut dyn Gpu, context: &mut Context) {
558 for draw in &self.draws {
559 draw.mesh.upload(gpu, context);
560 }
561 }
562
563 pub(crate) fn render(&mut self, gpu: &mut dyn Gpu, parameters: &mut PaintParameters<'_>) -> bool {
570 if self.draws.is_empty() || parameters.pass_target().is_none() {
571 return true;
572 }
573 let composited = (self.composite.is_some() && parameters.pass == RenderPass::TRANSLUCENT)
574 .then(|| parameters.begin_composited(gpu, self.id))
575 .flatten();
576 let drawing = composited.as_ref().is_none_or(Composited::draws);
577 let draws = if drawing { &self.draws[..] } else { &[] };
578 let both = RenderPass::OPAQUE | RenderPass::TRANSLUCENT;
582 let drawn_in =
583 if parameters.opaque_pass_enabled_for_layer() { RenderPass::OPAQUE } else { RenderPass::TRANSLUCENT };
584 let pass = parameters.pass;
585 let skip = if pass & both != RenderPass::NONE && pass != drawn_in { both } else { RenderPass::NONE };
586 let in_pass = |draw: &&GroupDraw| {
587 draw.pass & pass != RenderPass::NONE && (skip == RenderPass::NONE || draw.pass & skip != skip)
588 };
589 let (volume, volume_clips) =
590 draws.iter().filter(in_pass).fold((false, false), |(volume, clips), draw| match draw.depth {
591 Depth::Volume { .. } => (true, clips || draw.clip),
592 _ => (volume, clips),
593 });
594 let mut volume_stencil = None;
595 if volume {
596 volume_stencil = volume_clips.then(|| parameters.stencil_mode_for_3d(gpu));
597 } else if drawing && !self.stencil_tiles.is_empty() {
598 parameters.render_tile_clipping_masks(gpu, &self.stencil_tiles);
599 }
600 let mut drawn = true;
601 for (index, draw) in draws.iter().enumerate().filter(|(_, draw)| in_pass(draw)) {
602 let tile_stencil = || match draw.tile.filter(|_| draw.clip) {
603 Some(tile) => parameters.stencil_mode_for_clipping(&tile.to_unwrapped()),
604 None => StencilMode::disabled(),
605 };
606 let (depth, stencil) = match draw.depth {
607 Depth::Off => (DepthMode::disabled(), tile_stencil()),
608 Depth::Layer { sub_layer, mask } => {
609 (parameters.depth_mode_for_sublayer(sub_layer, mask), tile_stencil())
610 }
611 Depth::Volume { mask } => (
612 parameters.depth_mode_for_3d(mask),
613 volume_stencil.filter(|_| draw.clip).unwrap_or_else(StencilMode::disabled),
614 ),
615 };
616 let Some(mut objects) = parameters.context.take_mesh(&draw.mesh) else {
617 debug_assert!(false, "a draw of a mesh the context doesn't have");
618 continue;
619 };
620 drawn &= objects.draw(gpu, parameters, &mut self.buffers, depth, stencil, index as u32);
621 parameters.context.put_mesh(&draw.mesh, objects);
622 }
623 if let Some(composited) = composited {
624 parameters.end_composited(gpu, composited);
625 }
626 drawn
627 }
628}
629
630#[derive(Clone, Copy, Debug, PartialEq, Eq)]
632pub enum LendAt {
633 BeforePasses,
636 Pass,
639}
640
641pub struct Lent<'a> {
644 pub raw: &'a mut dyn Any,
651 pub frame: &'a LayerFrame<'a>,
653 pub projection: &'a FrameProjection,
655 pub layer_depth: Option<f64>,
658 pub after_gpu: &'a AfterGpu,
662}
663
664#[derive(Default)]
671pub struct Draws {
672 main: Group,
673 targets: Vec<(Target, Group)>,
678 frame_targets: usize,
680 lends: [bool; 2],
683 started: bool,
686}
687
688impl Draws {
689 pub fn main(&mut self) -> &mut Group {
692 &mut self.main
693 }
694
695 pub fn target(&mut self, target: &Target) -> &mut Group {
703 let frame = self.frame_targets;
704 let at = match self.targets.iter().position(|(given, _)| given == target) {
705 Some(at) if at < frame => return &mut self.targets[at].1,
706 Some(at) => at,
707 None => {
708 self.targets.push((target.clone(), Group::default()));
709 self.targets.len() - 1
710 }
711 };
712 self.targets.swap(at, frame);
713 self.frame_targets += 1;
714 &mut self.targets[frame].1
715 }
716
717 pub fn lend(&mut self, at: LendAt) -> &mut Self {
721 self.lends[at as usize] = true;
722 self
723 }
724
725 #[must_use]
728 pub(crate) fn lends(&self, at: LendAt) -> bool {
729 self.lends[at as usize]
730 }
731
732 pub(crate) fn begin(&mut self) {
735 self.started = true;
736 self.reset();
737 }
738
739 pub(crate) fn clear(&mut self) {
742 self.reset();
743 self.targets.clear();
744 }
745
746 fn reset(&mut self) {
748 for (_, group) in &mut self.targets {
749 group.clear();
750 }
751 self.frame_targets = 0;
752 self.lends = [false; 2];
753 self.main.clear();
754 }
755
756 pub(crate) fn finish(
761 &mut self,
762 gpu: &mut dyn Gpu,
763 context: &mut Context,
764 paint_params: Option<&GlobalPaintParamsUBO>,
765 tiles: impl IntoIterator<Item = UnwrappedTileID>,
766 ) {
767 self.finish_targets(gpu, context, paint_params);
768 self.main.finish(gpu, context, paint_params, tiles);
769 }
770
771 fn finish_targets(
774 &mut self,
775 gpu: &mut dyn Gpu,
776 context: &mut Context,
777 paint_params: Option<&GlobalPaintParamsUBO>,
778 ) {
779 self.targets.truncate(self.frame_targets);
780 for (target, group) in &mut self.targets {
781 debug_assert!(group.composite.is_none(), "draws into a target that composite");
782 group.finish(gpu, context, paint_params, []);
783 context.declare_target(target);
784 }
785 }
786
787 pub(crate) fn visit_groups<'a>(&'a self, visit: &mut dyn FnMut(&'a Group)) {
790 if self.started {
791 visit(&self.main);
792 }
793 }
794
795 pub(crate) fn upload(&self, gpu: &mut dyn Gpu, context: &mut Context) {
798 if self.started {
799 for (_, group) in &self.targets[..self.frame_targets] {
800 group.upload(gpu, context);
801 }
802 self.main.upload(gpu, context);
803 }
804 }
805
806 pub(crate) fn render_targets(&mut self, gpu: &mut dyn Gpu, parameters: &mut PaintParameters<'_>) {
809 for (target, group) in &mut self.targets[..self.frame_targets] {
810 target.render(gpu, parameters, |gpu, parameters| group.render(gpu, parameters));
811 }
812 }
813
814 pub(crate) fn render(
818 &mut self,
819 gpu: &mut dyn Gpu,
820 parameters: &mut PaintParameters<'_>,
821 order: DrawOrder,
822 in_pass: Option<&mut InPass<'_>>,
823 ) {
824 if self.started {
825 self.main.render(gpu, parameters);
826 if let Some(in_pass) = in_pass {
827 in_pass(gpu, parameters);
828 }
829 order.step(parameters);
830 }
831 }
832}
833
834impl Draws {
835 pub(crate) fn skip(&self, parameters: &mut PaintParameters<'_>, order: DrawOrder) {
838 if self.started {
839 order.step(parameters);
840 }
841 }
842}
843
844pub(crate) type InPass<'a> = dyn FnMut(&mut dyn Gpu, &mut PaintParameters<'_>) + 'a;
846
847pub(crate) struct SingleDraw {
850 pub(crate) mesh: Mesh,
851 draw: Draw,
853 group: Group,
854}
855
856impl SingleDraw {
857 #[must_use]
860 pub(crate) fn new(mesh: Mesh, draw: Draw) -> SingleDraw {
861 SingleDraw { mesh, draw, group: Group::default() }
862 }
863
864 pub(crate) fn block<T: UniformBlock>(&mut self, block: &T) -> &mut Self {
868 self.group.block(block);
869 self
870 }
871
872 pub(crate) fn write<B: DrawBlocks>(&mut self, gpu: &mut dyn Gpu, context: &mut Context, blocks: B) {
875 let SingleDraw { mesh, draw, group } = self;
876 group.push(mesh, *draw, blocks);
877 group.finish(gpu, context, None, []);
878 }
879
880 pub(crate) fn upload(&self, gpu: &mut dyn Gpu, context: &mut Context) {
882 self.group.upload(gpu, context);
883 }
884
885 pub(crate) fn render(&mut self, gpu: &mut dyn Gpu, parameters: &mut PaintParameters<'_>) -> bool {
888 self.group.render(gpu, parameters)
889 }
890}
891
892#[derive(Default)]
898pub(crate) struct DrawGroups {
899 pub(crate) count: usize,
900 pub(crate) clip_tiles: Vec<UnwrappedTileID>,
901 pub(crate) stencil_clear: StencilClear,
904 pub(crate) composited: Vec<(SimpleIdentity, Composite)>,
905}
906
907impl DrawGroups {
908 pub(crate) fn of<'a>(
913 visit_groups: impl FnOnce(&mut dyn FnMut(&'a Group)),
914 stencil_clear_tiles: impl FnOnce(&[UnwrappedTileID]) -> StencilClear,
915 ) -> DrawGroups {
916 let mut groups = DrawGroups::default();
917 let mut last: Option<&[UnwrappedTileID]> = None;
918 visit_groups(&mut |group| {
919 groups.count += 1;
920 if let Some(tiles) = group.stencil_tiles()
921 && last.is_none_or(|last| last != tiles)
922 {
923 groups.clip_tiles.extend(tiles.iter().copied());
924 last = Some(tiles);
925 }
926 groups.composited.extend(group.composited());
927 });
928 groups.stencil_clear = stencil_clear_tiles(&groups.clip_tiles);
929 groups.clip_tiles.extend_from_slice(&groups.stencil_clear);
930 if !groups.clip_tiles.is_sorted() {
931 groups.clip_tiles.sort_unstable();
932 }
933 groups.clip_tiles.dedup();
934 groups
935 }
936
937 #[must_use]
940 pub(crate) fn depth_range_size(&self) -> f32 {
941 depth_range_size(self.count)
942 }
943}
944
945#[must_use]
948pub(crate) fn depth_range_size(groups: usize) -> f32 {
949 1.0 - ((groups + 2) * PaintParameters::NUM_SUBLAYERS as usize) as f32 * PaintParameters::DEPTH_EPSILON
950}
951
952#[derive(Clone, Copy, Debug, PartialEq, Eq)]
956pub enum DrawOrder {
957 BottomUp,
960 TopDown,
963}
964
965impl DrawOrder {
966 pub(crate) fn step(self, parameters: &mut PaintParameters<'_>) {
968 match self {
969 DrawOrder::BottomUp => parameters.current_layer = parameters.current_layer.saturating_sub(1),
970 DrawOrder::TopDown => parameters.current_layer += 1,
971 }
972 }
973}