cranpose-render-wgpu 0.26.0

GPU renderer for Cranpose scenes through wgpu
Documentation
use cranpose_ui_graphics::TileMode;

use crate::render::TargetRect;

pub(crate) fn scissor_in_target(
    scissor: (f32, f32, f32, f32),
    target_size: (u32, u32),
    segment_offset: [f32; 2],
) -> Option<(u32, u32, u32, u32)> {
    let (x, y, width, height) = scissor;
    let left = (x - segment_offset[0]).floor().max(0.0);
    let top = (y - segment_offset[1]).floor().max(0.0);
    let right = (x + width - segment_offset[0])
        .ceil()
        .min(target_size.0 as f32);
    let bottom = (y + height - segment_offset[1])
        .ceil()
        .min(target_size.1 as f32);
    if right <= left || bottom <= top {
        return None;
    }
    Some((
        left as u32,
        top as u32,
        (right - left) as u32,
        (bottom - top) as u32,
    ))
}

pub(crate) fn intersect_scissors(
    a: Option<(u32, u32, u32, u32)>,
    b: Option<(u32, u32, u32, u32)>,
) -> Option<Option<(u32, u32, u32, u32)>> {
    match (a, b) {
        (None, None) => Some(None),
        (Some(rect), None) | (None, Some(rect)) => Some(Some(rect)),
        (Some(a), Some(b)) => intersect_target_rects(a, b).map(Some),
    }
}

/// The pixels both scissors keep, `None` when they share none.
pub(crate) fn intersect_target_rects(a: TargetRect, b: TargetRect) -> Option<TargetRect> {
    let left = a.0.max(b.0);
    let top = a.1.max(b.1);
    let right = (a.0 + a.2).min(b.0 + b.2);
    let bottom = (a.1 + a.3).min(b.1 + b.3);
    (right > left && bottom > top).then(|| (left, top, right - left, bottom - top))
}

/// Whether `inner` lies within `outer`.
pub(crate) fn target_rect_within(inner: TargetRect, outer: TargetRect) -> bool {
    inner.0 >= outer.0
        && inner.1 >= outer.1
        && inner.0 + inner.2 <= outer.0 + outer.2
        && inner.1 + inner.3 <= outer.1 + outer.3
}

// The pixels between the whole-pixel `edges` (left, top, right, bottom),
// cut to a `width` by `height` target; `None` when they hold none.
pub(crate) fn target_rect_between(
    edges: [f32; 4],
    (width, height): (u32, u32),
) -> Option<TargetRect> {
    let (width, height) = (width as f32, height as f32);
    let left = edges[0].clamp(0.0, width);
    let top = edges[1].clamp(0.0, height);
    let right = edges[2].clamp(0.0, width);
    let bottom = edges[3].clamp(0.0, height);
    if right <= left || bottom <= top {
        return None;
    }
    Some((
        left as u32,
        top as u32,
        (right - left) as u32,
        (bottom - top) as u32,
    ))
}

pub(crate) fn target_rects_overlap(a: TargetRect, b: TargetRect) -> bool {
    a.0 < b.0 + b.2 && b.0 < a.0 + a.2 && a.1 < b.1 + b.3 && b.1 < a.1 + a.3
}

pub(crate) fn scaled_scissor(
    scissor: Option<(u32, u32, u32, u32)>,
    scale_x: f32,
    scale_y: f32,
    width: u32,
    height: u32,
) -> Option<(u32, u32, u32, u32)> {
    let (x, y, w, h) = scissor?;
    if scale_x <= 1.0 && scale_y <= 1.0 {
        return scissor;
    }
    let left = ((x as f32 / scale_x).floor() as u32).min(width);
    let top = ((y as f32 / scale_y).floor() as u32).min(height);
    let right = (((x + w) as f32 / scale_x).ceil() as u32).min(width);
    let bottom = (((y + h) as f32 / scale_y).ceil() as u32).min(height);
    Some((
        left,
        top,
        right.saturating_sub(left),
        bottom.saturating_sub(top),
    ))
}

fn axis_span(start: i64, end: i64, margin: u32, len: u32, wraps: bool) -> (u32, u32) {
    let (low, high) = (start - i64::from(margin), end + i64::from(margin));
    if wraps && (low < 0 || high > i64::from(len)) {
        return (0, len.max(1));
    }
    let low = low.max(0) as u32;
    (low, (high.min(i64::from(len)) as u32).max(low + 1))
}

pub(crate) fn read_scissor(
    source: (u32, u32, u32, u32),
    scratch: (u32, u32, u32, u32),
    read: Option<(u32, u32, u32, u32)>,
    margin: (u32, u32),
    tile_mode: TileMode,
) -> (u32, u32, u32, u32) {
    let wraps = tile_mode == TileMode::Repeated;
    let Some((ux, uy, uw, uh)) = read else {
        return scratch;
    };
    let fx = scratch.2 as f32 / source.2.max(1) as f32;
    let fy = scratch.3 as f32 / source.3.max(1) as f32;
    let span = |start: u32, end: u32, origin: u32, factor: f32| {
        (
            ((start as f32 - origin as f32) * factor).floor() as i64,
            ((end as f32 - origin as f32) * factor).ceil() as i64,
        )
    };
    let (x0, x1) = span(ux, ux + uw, source.0, fx);
    let (y0, y1) = span(uy, uy + uh, source.1, fy);
    let (left, right) = axis_span(x0, x1, margin.0, scratch.2, wraps);
    let (top, bottom) = axis_span(y0, y1, margin.1, scratch.3, wraps);
    (
        scratch.0 + left,
        scratch.1 + top,
        right - left,
        bottom - top,
    )
}

pub(crate) fn widened_scissor(
    scissor: Option<(u32, u32, u32, u32)>,
    margin: (u32, u32),
    bounds: (u32, u32),
    tile_mode: TileMode,
) -> Option<(u32, u32, u32, u32)> {
    let wraps = tile_mode == TileMode::Repeated;
    let (x, y, width, height) = scissor?;
    let (left, right) = axis_span(
        i64::from(x),
        i64::from(x + width),
        margin.0,
        bounds.0,
        wraps,
    );
    let (top, bottom) = axis_span(
        i64::from(y),
        i64::from(y + height),
        margin.1,
        bounds.1,
        wraps,
    );
    Some((left, top, right - left, bottom - top))
}

#[cfg(test)]
#[path = "tests/scissor_tests.rs"]
mod tests;