use std::rc::Rc;
use glam::{DMat4, DVec4};
use mln_gfx::cull_face_mode::CullFaceMode;
use mln_gfx::gpu::{ColorAttachment, DepthStencilAttachment, Gpu, GpuError, LoadOp, PassDesc, Scissor, TargetFormats};
use mln_gfx::shaders::layer_ubo::GlobalPaintParamsUBO;
use mln_gfx::stencil_mode::Test;
use mln_gfx::types::{CullFaceWindingType, DepthFunctionType, StencilOpType};
use mln_gfx::{DepthMaskType, DepthMode, ScissorRect, Size, StencilMode};
use mln_style::collections::VecMap;
use mln_style::constants::EXTENT;
use mln_style::tile_id::CanonicalTileID;
use smallvec::SmallVec;
use crate::camera::{ProjectionData, View, ViewportMode};
use crate::draw::context::Context;
use crate::draw::draws::{DrawsInto, LayerFrame};
use crate::draw::identity::SimpleIdentity;
use crate::render::clip_masks::ClipMasks;
use crate::render::frame::Frame;
use crate::render::layer_composite::LayerComposite;
use crate::render::pass::RenderPass;
use crate::render::terrain::FrameGround;
use crate::tile::UnwrappedTileID;
pub(crate) type StencilClear = SmallVec<[UnwrappedTileID; 2]>;
#[must_use = "a composited draw group ends with `PaintParameters::end_composited`"]
pub(crate) struct Composited {
group: SimpleIdentity,
frame: OpenPass,
began: bool,
frame_stencil: Option<(VecMap<UnwrappedTileID, i32>, i32)>,
unended: Unended,
}
struct Unended;
impl Drop for Unended {
fn drop(&mut self) {
debug_assert!(std::thread::panicking(), "a composited draw group ended without `end_composited`");
}
}
impl Composited {
#[must_use]
pub(crate) fn draws(&self) -> bool {
self.began
}
}
pub(crate) struct TransformParameters {
pub(crate) proj_matrix: DMat4,
pub(crate) aligned_proj_matrix: DMat4,
pub(crate) near_clipped_proj_matrix: DMat4,
pub(crate) near_clipped_near_z: f64,
pub(crate) view: View,
}
impl TransformParameters {
pub(crate) fn new(view: &View) -> Self {
let state = view.clone();
let proj_matrix = state.projection_matrix_with(state.near_z(), false);
let aligned_proj_matrix = state.projection_matrix_with(state.near_z(), true);
let near_clipped_near_z = f64::from((0.1 * state.camera_to_center_distance()) as u16);
let near_clipped_proj_matrix = state.projection_matrix_with(near_clipped_near_z, false);
TransformParameters {
proj_matrix,
aligned_proj_matrix,
near_clipped_proj_matrix,
near_clipped_near_z,
view: state,
}
}
pub(crate) fn for_drape(view: &View, tile: &UnwrappedTileID) -> Self {
let tile_matrix = view.tile_matrix(tile);
let (x, y) = (tile_matrix.w_axis.x, tile_matrix.w_axis.y);
let side = tile_matrix.x_axis.x * f64::from(EXTENT);
let proj_matrix = DMat4::from_cols(
DVec4::new(2.0 / side, 0.0, 0.0, 0.0),
DVec4::new(0.0, -2.0 / side, 0.0, 0.0),
DVec4::ZERO,
DVec4::new(-1.0 - 2.0 * x / side, 1.0 + 2.0 * y / side, 0.0, 1.0),
);
TransformParameters {
proj_matrix,
aligned_proj_matrix: proj_matrix,
near_clipped_proj_matrix: proj_matrix,
near_clipped_near_z: 0.0,
view: view.clone(),
}
}
#[must_use]
pub(crate) fn projection_data_for_tile(&self, tile_id: &UnwrappedTileID, aligned: bool) -> ProjectionData {
self.view.projection_data_with(tile_id, if aligned { &self.aligned_proj_matrix } else { &self.proj_matrix })
}
}
#[derive(Clone, Copy)]
pub(crate) struct DrawTarget {
pub(crate) size: Size,
pub(crate) targets: TargetFormats,
pub(crate) scissor: Option<Scissor>,
}
pub(crate) struct FrameStencil(VecMap<UnwrappedTileID, i32>, i32);
#[derive(Clone, Copy)]
pub(crate) struct OpenPass {
pub(crate) label: &'static str,
pub(crate) color: Option<ColorAttachment>,
pub(crate) depth_stencil: Option<DepthStencilAttachment>,
pub(crate) size: Size,
pub(crate) targets: TargetFormats,
}
impl OpenPass {
fn loading(&self, stencil: LoadOp<u32>) -> OpenPass {
OpenPass {
color: self.color.map(|color| ColorAttachment { load: LoadOp::Load, ..color }),
depth_stencil: self.depth_stencil.map(|depth_stencil| DepthStencilAttachment {
depth: LoadOp::Load,
stencil,
..depth_stencil
}),
..*self
}
}
fn desc(&self) -> PassDesc<'static> {
PassDesc { label: self.label, color: self.color, depth_stencil: self.depth_stencil }
}
}
pub struct PaintParameters<'a> {
pub(crate) context: &'a mut Context,
open_pass: Option<OpenPass>,
pub(crate) transform_params: &'a TransformParameters,
pub(crate) view: &'a View,
pub(crate) camera_moving: bool,
pub(crate) light: &'a crate::render::light::EvaluatedLight,
pub(crate) pass: RenderPass,
pub(crate) pixel_ratio: f32,
pub(crate) renderable_size: Size,
pub(crate) pixels_to_gl_units: [f32; 2],
pub(crate) clip_masks: &'a mut ClipMasks,
pub(crate) layer_composite: &'a mut LayerComposite,
pub(crate) composites_with_frame_depth: bool,
tile_clipping_mask_ids: VecMap<UnwrappedTileID, i32>,
next_stencil_id: i32,
drape_tile: Option<UnwrappedTileID>,
pub(crate) drape_lit: bool,
pub(crate) stencil_clear: StencilClear,
pub(crate) current_layer: u32,
pub(crate) depth_range_size: f32,
pub(crate) terrain: bool,
pub(crate) ground: Option<Rc<FrameGround>>,
pub(crate) opaque_pass_cutoff: u32,
pub(crate) symbol_fade_change: f32,
pub(crate) scissor_rect: ScissorRect,
pub(crate) lent_projection: Option<crate::camera::FrameProjection>,
}
impl<'a> PaintParameters<'a> {
pub(crate) const NUM_SUBLAYERS: i32 = 3;
pub(crate) const DEPTH_EPSILON: f32 = 1.0 / (1 << 16) as f32;
pub(crate) const MAX_STENCIL_VALUE: i32 = 255;
#[expect(clippy::too_many_arguments, reason = "the renderer's GPU state, the frame and where it draws")]
pub(crate) fn new(
context: &'a mut Context,
clip_masks: &'a mut ClipMasks,
layer_composite: &'a mut LayerComposite,
composites_with_frame_depth: bool,
frame: &'a Frame,
pixel_ratio: f32,
renderable_size: Size,
scissor_rect: ScissorRect,
) -> Self {
let transform_params = &frame.transform;
let state = &transform_params.view;
let mut pixels_to_gl_units = [2.0f32 / state.size().width as f32, -2.0f32 / state.size().height as f32];
if state.viewport_mode() == ViewportMode::FlippedY {
pixels_to_gl_units[1] *= -1.0;
}
PaintParameters {
context,
open_pass: None,
transform_params,
view: state,
camera_moving: frame.camera_moving,
light: &frame.light,
pass: RenderPass::Opaque,
pixel_ratio,
renderable_size,
pixels_to_gl_units,
clip_masks,
layer_composite,
composites_with_frame_depth,
tile_clipping_mask_ids: VecMap::new(),
next_stencil_id: 1,
drape_tile: None,
drape_lit: false,
stencil_clear: StencilClear::new(),
current_layer: 0,
depth_range_size: 1.0,
terrain: frame.terrain,
ground: None,
opaque_pass_cutoff: 0,
symbol_fade_change: frame.symbol_fade_change,
scissor_rect,
lent_projection: None,
}
}
pub(crate) fn cull_back_faces(&self) -> CullFaceMode {
let turned = self.view.viewport_mode() == ViewportMode::FlippedY && !self.view.is_globe();
let winding = if turned { CullFaceWindingType::Clockwise } else { CullFaceWindingType::CounterClockwise };
CullFaceMode { winding, ..CullFaceMode::back_ccw() }
}
pub(crate) fn drape<'b>(
&'b mut self,
transform: &'b TransformParameters,
clip_masks: &'b mut ClipMasks,
layer_composite: &'b mut LayerComposite,
size: Size,
tile: UnwrappedTileID,
) -> PaintParameters<'b> {
let proj_matrix = &transform.proj_matrix;
PaintParameters {
context: &mut *self.context,
open_pass: None,
transform_params: transform,
view: &transform.view,
camera_moving: self.camera_moving,
light: self.light,
pass: RenderPass::Opaque,
pixel_ratio: self.pixel_ratio,
renderable_size: size,
pixels_to_gl_units: [proj_matrix.x_axis.x as f32, proj_matrix.y_axis.y as f32],
clip_masks,
layer_composite,
composites_with_frame_depth: false,
tile_clipping_mask_ids: VecMap::new(),
next_stencil_id: 1,
drape_tile: Some(tile),
drape_lit: false,
stencil_clear: StencilClear::new(),
current_layer: 0,
depth_range_size: 1.0,
terrain: false,
ground: None,
opaque_pass_cutoff: 0,
symbol_fade_change: 1.0,
scissor_rect: ScissorRect { x: 0, y: 0, width: size.width, height: size.height },
lent_projection: None,
}
}
#[must_use]
pub(crate) fn frame(&self) -> LayerFrame<'a> {
LayerFrame {
view: self.view,
transform_params: self.transform_params,
camera_moving: self.camera_moving,
light: self.light,
pixel_ratio: self.pixel_ratio,
renderable_size: self.renderable_size,
pixels_to_gl_units: self.pixels_to_gl_units,
terrain: self.terrain,
composites_with_frame_depth: self.composites_with_frame_depth,
ground: self.ground.clone(),
into: if self.drape_tile.is_some() { DrawsInto::Drape { lit: self.drape_lit } } else { DrawsInto::Map },
}
}
#[must_use]
pub(crate) fn global_paint_params(&self) -> GlobalPaintParamsUBO {
GlobalPaintParamsUBO {
units_to_pixels: [1.0 / self.pixels_to_gl_units[0], 1.0 / self.pixels_to_gl_units[1]],
world_size: [self.renderable_size.width as f32, self.renderable_size.height as f32],
camera_to_center_distance: self.view.camera_to_center_distance() as f32,
symbol_fade_change: self.symbol_fade_change,
aspect_ratio: self.view.size().aspect_ratio(),
pixel_ratio: self.pixel_ratio,
map_zoom: self.view.zoom() as f32,
pad1: 0.0,
pad2: 0.0,
pad3: 0.0,
}
}
#[must_use]
pub(crate) fn own_paint_params(&self) -> Option<GlobalPaintParamsUBO> {
self.drape_tile.is_some().then(|| self.global_paint_params())
}
pub(crate) fn pass_target(&self) -> Option<(Size, TargetFormats)> {
self.open_pass.map(|pass| (pass.size, pass.targets))
}
pub(crate) fn draw_target(&self) -> Option<DrawTarget> {
let (size, targets) = self.pass_target()?;
Some(DrawTarget { size, targets, scissor: clamped_scissor(self.scissor_rect, size) })
}
pub(crate) fn begin_pass(&mut self, gpu: &mut dyn Gpu, pass: OpenPass) -> Result<(), GpuError> {
gpu.begin_pass(&pass.desc())?;
self.open_pass = Some(pass);
Ok(())
}
pub(crate) fn end_pass(&mut self, gpu: &mut dyn Gpu) -> Result<(), GpuError> {
match self.open_pass.take() {
Some(_) => gpu.end_pass(),
None => Ok(()),
}
}
fn restart_pass(&mut self, gpu: &mut dyn Gpu, stencil: LoadOp<u32>) {
let Some(pass) = self.open_pass else {
return;
};
let restarted = self.end_pass(gpu).and_then(|()| self.begin_pass(gpu, pass.loading(stencil)));
if let Err(error) = restarted {
self.context.fail(format!("Restarting the {} pass failed: {error}", pass.label));
}
}
pub(crate) fn begin_composited(&mut self, gpu: &mut dyn Gpu, group: SimpleIdentity) -> Option<Composited> {
let frame = self.open_pass.filter(|_| self.layer_composite.composites(group))?;
let composite = self.layer_composite.pass(group, &frame)?;
let own_stencil = !self.layer_composite.draws_with_frame_depth(group);
let mut drawn = self.end_pass(gpu);
if drawn.is_ok()
&& let Some(backdrop) = self.layer_composite.backdrop_pass(group)
{
drawn = self.begin_pass(gpu, backdrop);
if drawn.is_ok() {
if let Err(error) = self.copy_frame(gpu) {
self.context.fail(format!("Copying the frame failed: {error}"));
}
drawn = self.end_pass(gpu);
}
}
drawn = drawn.and_then(|()| self.begin_pass(gpu, composite));
let began = drawn.is_ok();
if let Err(error) = drawn {
self.context.fail(format!("Beginning the layer composite's pass failed: {error}"));
}
let frame_stencil = (began && own_stencil).then(|| {
(std::mem::take(&mut self.tile_clipping_mask_ids), std::mem::replace(&mut self.next_stencil_id, 1))
});
Some(Composited { group, frame, began, frame_stencil, unended: Unended })
}
pub(crate) fn begin_own_stencil(&mut self) -> FrameStencil {
FrameStencil(std::mem::take(&mut self.tile_clipping_mask_ids), std::mem::replace(&mut self.next_stencil_id, 1))
}
pub(crate) fn end_own_stencil(&mut self, frame: FrameStencil) {
FrameStencil(self.tile_clipping_mask_ids, self.next_stencil_id) = frame;
}
fn copy_frame(&mut self, gpu: &mut dyn Gpu) -> Result<(), GpuError> {
match self.draw_target() {
Some(target) => self.layer_composite.copy_frame(gpu, target, self.context),
None => Ok(()),
}
}
pub(crate) fn end_composited(&mut self, gpu: &mut dyn Gpu, composited: Composited) {
let Composited { group, frame, began, frame_stencil, unended } = composited;
std::mem::forget(unended);
if let Some((tile_clipping_mask_ids, next_stencil_id)) = frame_stencil {
self.tile_clipping_mask_ids = tile_clipping_mask_ids;
self.next_stencil_id = next_stencil_id;
}
if began && let Err(error) = self.end_pass(gpu) {
self.context.fail(format!("Ending the layer composite's pass failed: {error}"));
}
if let Err(error) = self.begin_pass(gpu, frame.loading(LoadOp::Load)) {
self.context.fail(format!("Resuming the {} pass failed: {error}", frame.label));
} else if began && let Some(target) = self.draw_target() {
let depth = if self.layer_composite.draws_with_frame_depth(group) {
DepthMode::disabled()
} else {
self.depth_mode_for_sublayer(0, DepthMaskType::ReadOnly)
};
self.layer_composite.draw(gpu, target, depth, self.context, group);
}
}
pub(crate) fn present_frame(&mut self, gpu: &mut dyn Gpu, present: OpenPass) -> Result<(), GpuError> {
if self.layer_composite.frame_texture().is_none() {
return Ok(());
}
self.end_pass(gpu)?;
self.begin_pass(gpu, present)?;
self.copy_frame(gpu)
}
#[must_use]
pub(crate) fn opaque_pass_enabled_for_layer(&self) -> bool {
self.current_layer >= self.opaque_pass_cutoff && !self.view.is_globe() && !self.terrain
}
#[must_use]
pub(crate) fn depth_mode_for_sublayer(&self, n: u8, mask: DepthMaskType) -> DepthMode {
if !self.opaque_pass_enabled_for_layer() {
return DepthMode::disabled();
}
let steps =
(1u32.wrapping_add(self.current_layer)).wrapping_mul(Self::NUM_SUBLAYERS as u32).wrapping_add(u32::from(n));
let depth = self.depth_range_size + steps as f32 * Self::DEPTH_EPSILON;
DepthMode { func: DepthFunctionType::LessEqual, mask, range: (depth, depth) }
}
#[must_use]
pub(crate) fn layer_depth(&self) -> f32 {
let steps = (1u32.wrapping_add(self.current_layer)).wrapping_mul(Self::NUM_SUBLAYERS as u32);
self.depth_range_size + steps as f32 * Self::DEPTH_EPSILON
}
#[must_use]
pub(crate) fn depth_mode_for_3d(&self, mask: DepthMaskType) -> DepthMode {
DepthMode { func: DepthFunctionType::LessEqual, mask, range: (0.0, self.depth_range_size) }
}
pub(crate) fn clear_stencil(&mut self, gpu: &mut dyn Gpu) {
self.next_stencil_id = 1;
self.tile_clipping_mask_ids.clear();
if self.context.caps().clear_stencil_with_pass {
self.restart_pass(gpu, LoadOp::Clear(0));
} else {
let masks: SmallVec<[_; 2]> = match self.drape_tile {
Some(tile) => SmallVec::from_elem((tile, 0), 1),
None => self.stencil_clear.iter().map(|tile| (*tile, 0)).collect(),
};
self.draw_clip_masks(gpu, &masks);
}
self.context.rendering_stats().stencil_clears += 1;
}
#[must_use]
pub(crate) fn stencil_clear_tiles(&self, clip_tiles: &[UnwrappedTileID]) -> StencilClear {
let mut tiles = StencilClear::new();
if self.context.caps().clear_stencil_with_pass || clip_tiles.is_empty() {
return tiles;
}
if let Some(tile) = self.drape_tile {
tiles.push(tile);
return tiles;
}
for tile in clip_tiles {
if !tiles.iter().any(|world| world.wrap == tile.wrap) {
tiles.push(UnwrappedTileID::with_wrap(tile.wrap, CanonicalTileID::new(0, 0, 0)));
}
}
tiles.sort_unstable();
tiles
}
fn draw_clip_masks(&mut self, gpu: &mut dyn Gpu, masks: &[(UnwrappedTileID, i32)]) {
if let Some(target) = self.draw_target() {
self.clip_masks.draw(gpu, masks, target, self.context);
}
}
pub(crate) fn render_tile_clipping_masks(&mut self, gpu: &mut dyn Gpu, tile_ids: &[UnwrappedTileID]) {
let covered = self.tile_clipping_mask_ids.len() == tile_ids.len()
&& self.tile_clipping_mask_ids.keys().zip(tile_ids.iter()).all(|(a, b)| a == b);
if covered || self.pass_target().is_none() {
return;
}
self.tile_clipping_mask_ids.clear();
if self.next_stencil_id as usize + tile_ids.len() > Self::MAX_STENCIL_VALUE as usize {
self.clear_stencil(gpu);
}
let mut masks = Vec::with_capacity(tile_ids.len());
for tile_id in tile_ids {
if self.tile_clipping_mask_ids.contains_key(tile_id) {
continue;
}
self.tile_clipping_mask_ids.insert(*tile_id, self.next_stencil_id);
masks.push((*tile_id, self.next_stencil_id));
self.next_stencil_id += 1;
}
if !masks.is_empty() {
self.draw_clip_masks(gpu, &masks);
self.context.rendering_stats().stencil_updates += 1;
}
}
#[must_use]
pub(crate) fn stencil_mode_for_clipping(&self, tile_id: &UnwrappedTileID) -> StencilMode {
let it = self.tile_clipping_mask_ids.get(tile_id);
debug_assert!(it.is_some());
let id = it.copied().unwrap_or(0b0000_0000);
StencilMode {
test: Test::Equal { mask: 0b1111_1111 },
ref_: id,
mask: 0b0000_0000,
fail: StencilOpType::Keep,
depth_fail: StencilOpType::Keep,
pass: StencilOpType::Replace,
}
}
pub(crate) fn stencil_mode_for_3d(&mut self, gpu: &mut dyn Gpu) -> StencilMode {
if self.next_stencil_id + 1 > Self::MAX_STENCIL_VALUE {
self.clear_stencil(gpu);
}
self.tile_clipping_mask_ids.clear();
let id = self.next_stencil_id;
self.next_stencil_id += 1;
StencilMode {
test: Test::NotEqual { mask: 0b1111_1111 },
ref_: id,
mask: 0b1111_1111,
fail: StencilOpType::Keep,
depth_fail: StencilOpType::Keep,
pass: StencilOpType::Replace,
}
}
}
fn clamped_scissor(rect: ScissorRect, size: Size) -> Option<Scissor> {
let (x, y) = (rect.x as u32, rect.y as u32);
let width = if x < size.width { rect.width.min(size.width - x) } else { 0 };
let height = if y < size.height { rect.height.min(size.height - y) } else { 0 };
(width > 0 && height > 0).then_some(Scissor { x, y, width, height })
}