use crate::three_d::ThreeDRenderLimits;
use bevy_ecs::{
prelude::{Res, Resource},
system::SystemParam,
};
use sim_engine::{LogicalViewport, SceneBudget};
#[path = "feedback.rs"]
mod feedback;
pub use feedback::{
PresentationBudgetRejection, PresentationBudgetResource, PresentationBudgetStage,
PresentationFeedback,
};
#[derive(Debug, Clone, Copy, PartialEq, Resource)]
pub(crate) struct FrameViewportState(LogicalViewport);
impl FrameViewportState {
pub const fn new(viewport: LogicalViewport) -> Self {
Self(viewport)
}
pub const fn logical(self) -> LogicalViewport {
self.0
}
}
#[derive(SystemParam)]
pub struct FrameViewport<'w> {
state: Res<'w, FrameViewportState>,
}
impl FrameViewport<'_> {
pub fn logical(&self) -> LogicalViewport {
self.state.logical()
}
}
pub const DEFAULT_MAX_WORLD_CIRCLES: usize = 10_000;
pub const DEFAULT_MAX_WORLD_RECTANGLES: usize = 10_000;
pub const DEFAULT_MAX_WORLD_LINES: usize = 10_000;
pub const DEFAULT_MAX_SCREEN_RECTANGLES: usize = 256;
pub const DEFAULT_WORLD_SCENE_BUDGET: SceneBudget = SceneBudget::new(
10_000,
0,
2_000_000,
8 * 1024 * 1024,
16 * 1024 * 1024,
128 * 1024 * 1024,
10_000,
);
pub const DEFAULT_SCREEN_SCENE_BUDGET: SceneBudget =
SceneBudget::new(256, 65_536, 3_072, 256 * 1024, 512 * 1024, 512 * 1024, 256);
pub const DEFAULT_FRAME_LIMITS: FrameLimits = FrameLimits::new(
2,
10_000 + 256,
2_000_000 + 3_072,
128 * 1024 * 1024 + 512 * 1024,
0,
10_000 + 256,
);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameLimits {
max_passes: usize,
max_commands: usize,
max_vertices: usize,
max_upload_bytes: usize,
max_texture_bytes: usize,
max_draw_calls: usize,
}
impl FrameLimits {
pub const fn new(
max_passes: usize,
max_commands: usize,
max_vertices: usize,
max_upload_bytes: usize,
max_texture_bytes: usize,
max_draw_calls: usize,
) -> Self {
Self {
max_passes,
max_commands,
max_vertices,
max_upload_bytes,
max_texture_bytes,
max_draw_calls,
}
}
pub const fn max_passes(self) -> usize {
self.max_passes
}
pub const fn max_commands(self) -> usize {
self.max_commands
}
pub const fn max_vertices(self) -> usize {
self.max_vertices
}
pub const fn max_upload_bytes(self) -> usize {
self.max_upload_bytes
}
pub const fn max_texture_bytes(self) -> usize {
self.max_texture_bytes
}
pub const fn max_draw_calls(self) -> usize {
self.max_draw_calls
}
}
impl Default for FrameLimits {
fn default() -> Self {
DEFAULT_FRAME_LIMITS
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RenderLimits {
screen_scene_reuse: bool,
max_world_circles: usize,
max_world_rectangles: usize,
max_world_lines: usize,
max_screen_rectangles: usize,
max_screen_lines: usize,
max_screen_circles: usize,
max_screen_polylines: usize,
max_screen_polyline_points: usize,
max_screen_images: usize,
#[cfg(feature = "headless-text")]
max_screen_texts: usize,
#[cfg(feature = "headless-text")]
max_screen_text_bytes: usize,
#[cfg(feature = "headless-text")]
max_screen_text_glyphs: usize,
world_scene_budget: SceneBudget,
screen_scene_budget: SceneBudget,
frame_limits: FrameLimits,
three_d: ThreeDRenderLimits,
}
impl RenderLimits {
pub const fn new(
max_world_circles: usize,
world_scene_budget: SceneBudget,
frame_limits: FrameLimits,
) -> Self {
Self {
screen_scene_reuse: true,
max_world_circles,
max_world_rectangles: DEFAULT_MAX_WORLD_RECTANGLES,
max_world_lines: DEFAULT_MAX_WORLD_LINES,
max_screen_rectangles: DEFAULT_MAX_SCREEN_RECTANGLES,
max_screen_lines: 256,
max_screen_circles: 256,
max_screen_polylines: 256,
max_screen_polyline_points: 65_536,
max_screen_images: 0,
#[cfg(feature = "headless-text")]
max_screen_texts: 0,
#[cfg(feature = "headless-text")]
max_screen_text_bytes: 0,
#[cfg(feature = "headless-text")]
max_screen_text_glyphs: 0,
world_scene_budget,
screen_scene_budget: DEFAULT_SCREEN_SCENE_BUDGET,
frame_limits,
three_d: ThreeDRenderLimits::new(0, 0, 0),
}
}
pub const fn screen_scene_reuse(self) -> bool {
self.screen_scene_reuse
}
pub const fn with_screen_scene_reuse(mut self, enabled: bool) -> Self {
self.screen_scene_reuse = enabled;
self
}
pub const fn max_world_circles(self) -> usize {
self.max_world_circles
}
pub const fn max_world_rectangles(self) -> usize {
self.max_world_rectangles
}
pub const fn max_world_lines(self) -> usize {
self.max_world_lines
}
pub const fn max_screen_rectangles(self) -> usize {
self.max_screen_rectangles
}
pub const fn max_screen_lines(self) -> usize {
self.max_screen_lines
}
pub const fn max_screen_circles(self) -> usize {
self.max_screen_circles
}
pub const fn max_screen_polylines(self) -> usize {
self.max_screen_polylines
}
pub const fn with_max_screen_polylines(mut self, count: usize) -> Self {
self.max_screen_polylines = count;
self
}
pub const fn max_screen_polyline_points(self) -> usize {
self.max_screen_polyline_points
}
pub const fn with_max_screen_polyline_points(mut self, count: usize) -> Self {
self.max_screen_polyline_points = count;
self
}
pub const fn with_max_screen_lines(mut self, count: usize) -> Self {
self.max_screen_lines = count;
self
}
pub const fn with_max_screen_circles(mut self, count: usize) -> Self {
self.max_screen_circles = count;
self
}
pub const fn max_screen_images(self) -> usize {
self.max_screen_images
}
#[cfg(feature = "headless-text")]
pub const fn max_screen_texts(self) -> usize {
self.max_screen_texts
}
#[cfg(feature = "headless-text")]
pub const fn max_screen_text_bytes(self) -> usize {
self.max_screen_text_bytes
}
#[cfg(feature = "headless-text")]
pub const fn max_screen_text_glyphs(self) -> usize {
self.max_screen_text_glyphs
}
#[cfg(feature = "headless-text")]
pub const fn with_max_screen_texts(mut self, limit: usize) -> Self {
self.max_screen_texts = limit;
self
}
#[cfg(feature = "headless-text")]
pub const fn with_max_screen_text_bytes(mut self, limit: usize) -> Self {
self.max_screen_text_bytes = limit;
self
}
#[cfg(feature = "headless-text")]
pub const fn with_max_screen_text_glyphs(mut self, limit: usize) -> Self {
self.max_screen_text_glyphs = limit;
self
}
pub const fn three_d(self) -> ThreeDRenderLimits {
self.three_d
}
pub const fn with_three_d_render_limits(mut self, limits: ThreeDRenderLimits) -> Self {
self.three_d = limits;
self
}
pub const fn with_max_screen_images(mut self, limit: usize) -> Self {
self.max_screen_images = limit;
self
}
pub const fn with_frame_limits(mut self, limits: FrameLimits) -> Self {
self.frame_limits = limits;
self
}
pub const fn with_max_world_rectangles(mut self, limit: usize) -> Self {
self.max_world_rectangles = limit;
self
}
pub const fn with_max_world_lines(mut self, limit: usize) -> Self {
self.max_world_lines = limit;
self
}
pub const fn with_max_screen_rectangles(mut self, limit: usize) -> Self {
self.max_screen_rectangles = limit;
self
}
pub const fn with_screen_scene_budget(mut self, budget: SceneBudget) -> Self {
self.screen_scene_budget = budget;
self
}
pub const fn world_scene_budget(self) -> SceneBudget {
self.world_scene_budget
}
pub const fn screen_scene_budget(self) -> SceneBudget {
self.screen_scene_budget
}
pub const fn frame_limits(self) -> FrameLimits {
self.frame_limits
}
}
impl Default for RenderLimits {
fn default() -> Self {
Self::new(
DEFAULT_MAX_WORLD_CIRCLES,
DEFAULT_WORLD_SCENE_BUDGET,
DEFAULT_FRAME_LIMITS,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_the_first_slice_contract() {
let limits = RenderLimits::default();
let world = limits.world_scene_budget();
let screen = limits.screen_scene_budget();
let frame = limits.frame_limits();
assert_eq!(limits.max_world_circles(), 10_000);
assert_eq!(limits.max_world_rectangles(), 10_000);
assert_eq!(limits.max_world_lines(), 10_000);
assert_eq!(limits.max_screen_rectangles(), 256);
assert_eq!(world.max_commands(), 10_000);
assert_eq!(world.max_points(), 0);
assert_eq!(world.max_tessellated_vertices(), 2_000_000);
assert_eq!(world.max_retained_bytes(), 8 * 1024 * 1024);
assert_eq!(world.max_allocation_bytes(), 16 * 1024 * 1024);
assert_eq!(world.max_upload_bytes(), 128 * 1024 * 1024);
assert_eq!(world.max_draw_batches(), 10_000);
assert_eq!(screen.max_commands(), 256);
assert_eq!(screen.max_points(), 65_536);
assert_eq!(screen.max_tessellated_vertices(), 3_072);
assert_eq!(screen.max_retained_bytes(), 256 * 1024);
assert_eq!(screen.max_allocation_bytes(), 512 * 1024);
assert_eq!(screen.max_upload_bytes(), 512 * 1024);
assert_eq!(screen.max_draw_batches(), 256);
assert_eq!(frame.max_passes(), 2);
assert_eq!(
frame.max_commands(),
world.max_commands() + screen.max_commands()
);
assert_eq!(
frame.max_vertices(),
world.max_tessellated_vertices() + screen.max_tessellated_vertices()
);
assert_eq!(
frame.max_upload_bytes(),
world.max_upload_bytes() + screen.max_upload_bytes()
);
assert_eq!(frame.max_texture_bytes(), 0);
assert_eq!(
frame.max_draw_calls(),
world.max_draw_batches() + screen.max_draw_batches()
);
}
#[test]
fn rectangle_limit_builder_is_additive_and_allows_zero() {
let original = RenderLimits::new(7, DEFAULT_WORLD_SCENE_BUDGET, FrameLimits::default());
let changed = original.with_max_world_rectangles(0);
assert_eq!(changed.max_world_circles(), 7);
assert_eq!(changed.max_world_rectangles(), 0);
assert_eq!(changed.world_scene_budget(), original.world_scene_budget());
assert_eq!(changed.frame_limits(), original.frame_limits());
}
#[test]
fn line_limit_builder_is_additive_and_allows_zero() {
let original = RenderLimits::new(7, DEFAULT_WORLD_SCENE_BUDGET, FrameLimits::default());
let changed = original.with_max_world_lines(0);
assert_eq!(changed.max_world_circles(), 7);
assert_eq!(
changed.max_world_rectangles(),
original.max_world_rectangles()
);
assert_eq!(changed.max_world_lines(), 0);
assert_eq!(changed.world_scene_budget(), original.world_scene_budget());
assert_eq!(changed.frame_limits(), original.frame_limits());
}
#[test]
fn screen_limit_builders_preserve_world_and_custom_frame_limits() {
let frame = FrameLimits::new(1, 2, 3, 4, 5, 6);
let world = SceneBudget::new(7, 8, 9, 10, 11, 12, 13);
let original = RenderLimits::new(14, world, frame)
.with_max_world_rectangles(15)
.with_max_world_lines(16);
let zero = SceneBudget::new(0, 0, 0, 0, 0, 0, 0);
let changed = original
.with_max_screen_rectangles(0)
.with_screen_scene_budget(zero);
assert_eq!(changed.max_screen_rectangles(), 0);
assert_eq!(changed.screen_scene_budget(), zero);
assert_eq!(changed.max_world_circles(), original.max_world_circles());
assert_eq!(
changed.max_world_rectangles(),
original.max_world_rectangles()
);
assert_eq!(changed.max_world_lines(), original.max_world_lines());
assert_eq!(changed.world_scene_budget(), world);
assert_eq!(changed.frame_limits(), frame);
assert_eq!(
original.with_max_screen_rectangles(0).screen_scene_budget(),
original.screen_scene_budget()
);
assert_eq!(
original
.with_screen_scene_budget(zero)
.max_screen_rectangles(),
original.max_screen_rectangles()
);
}
#[test]
fn default_screen_budget_fits_every_allowed_square_rectangle()
-> Result<(), sim_engine::SceneError> {
use sim_engine::{
Color, LogicalScreenPosition, LogicalScreenVector, ScreenScene, ShapeStyle,
};
let limits = RenderLimits::default();
let mut scene = ScreenScene::with_budget(Color::TRANSPARENT, limits.screen_scene_budget())?;
for _ in 0..limits.max_screen_rectangles() {
scene.try_square_rect(
LogicalScreenPosition::default(),
LogicalScreenVector::new(16.0, 8.0),
ShapeStyle::filled(Color::WHITE),
)?;
}
assert_eq!(scene.command_count(), 256);
assert_eq!(scene.statistics().estimated_tessellated_vertices(), 3_072);
assert_eq!(scene.statistics().estimated_draw_batches(), 256);
Ok(())
}
}