use crate::composite::PixelRect;
use crate::effect_cost::{ARITHMETIC_UNITS, EffectCost, FETCH_UNITS, TRANSCENDENTAL_UNITS};
pub(crate) const PLANNING_THRESHOLD_UNIT_PIXELS: f64 = 64.0 * 1_048_576.0;
pub(crate) const MAX_BANDS: u32 = 64;
pub(crate) const MAX_TILE_COLUMNS: u32 = 16;
pub(crate) const MAX_FRAME_SUBMISSIONS: u32 = 128;
const SPREAD_SUBMISSIONS: u32 = MAX_FRAME_SUBMISSIONS / 4 * 3;
const _: () = assert!(
MAX_FRAME_SUBMISSIONS as usize <= crate::submission_clock::RECENT_SUBMISSIONS_KEPT,
"the submission clock keeps every submission of the latest frame"
);
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct FrameCost {
pub fetches: f64,
pub transcendentals: f64,
pub arithmetic: f64,
}
impl FrameCost {
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn unit_pixels(&self) -> f64 {
self.fetches * FETCH_UNITS as f64
+ self.transcendentals * TRANSCENDENTAL_UNITS as f64
+ self.arithmetic * ARITHMETIC_UNITS as f64
}
#[allow(clippy::cast_precision_loss)]
pub(crate) fn add(&mut self, cost: EffectCost, pixels: u64) {
let pixels = pixels as f64;
self.fetches += cost.fetches as f64 * pixels;
self.transcendentals += cost.transcendentals as f64 * pixels;
self.arithmetic += cost.arithmetic as f64 * pixels;
}
pub(crate) fn of(cost: EffectCost, pixels: u64) -> Self {
let mut charge = Self::default();
charge.add(cost, pixels);
charge
}
pub(crate) fn add_cost(&mut self, other: Self) {
self.fetches += other.fetches;
self.transcendentals += other.transcendentals;
self.arithmetic += other.arithmetic;
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct FrameSubmissions {
pub submissions: u32,
pub predicted_ms: Option<f64>,
pub cost: FrameCost,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct PassPlacement {
pub(crate) cut_before: bool,
pub(crate) pieces: Pieces,
pub(crate) pilot: Option<u32>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct Pieces {
pub(crate) rows: u32,
pub(crate) columns: u32,
}
impl Pieces {
pub(crate) const ONE: Self = Self {
rows: 1,
columns: 1,
};
fn within(budgets: f64, region: PixelRect) -> Self {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let needed = budgets.ceil().clamp(1.0, f64::from(u32::MAX)) as u32;
let rows = needed.min(MAX_BANDS).min(region.height.div_ceil(2)).max(1);
let columns = needed
.div_ceil(rows)
.min(MAX_TILE_COLUMNS)
.min(region.width.div_ceil(2))
.max(1);
Self {
rows: rows.min(needed.div_ceil(columns)),
columns,
}
}
fn at_most(self, most: u32) -> Self {
let most = most.max(1);
if self.count() <= most {
return self;
}
let rows = self.rows.min(most);
Self {
rows,
columns: (most / rows).clamp(1, self.columns),
}
}
pub(crate) const fn count(self) -> u32 {
self.rows.saturating_mul(self.columns)
}
pub(crate) fn rect(self, index: u32, region: PixelRect) -> PixelRect {
let (y, height) = even_split(region.y, region.height, index / self.columns, self.rows);
let (x, width) = even_split(region.x, region.width, index % self.columns, self.columns);
PixelRect {
x,
y,
width,
height,
}
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct Piece {
pub(crate) scissor: Option<PixelRect>,
pub(crate) first: bool,
}
impl Piece {
pub(crate) const fn first(scissor: Option<PixelRect>) -> Self {
Self {
scissor,
first: true,
}
}
}
fn even_split(start: u32, length: u32, part: u32, parts: u32) -> (u32, u32) {
let end = start.saturating_add(length);
let edge = |index: u32| -> u32 {
if index == 0 {
return start;
}
if index >= parts {
return end;
}
let offset = u64::from(length) * u64::from(index) / u64::from(parts);
let edge = start.saturating_add(u32::try_from(offset).unwrap_or(length)) & !1;
edge.clamp(start, end)
};
let first = edge(part);
(first, edge(part + 1).saturating_sub(first))
}
#[derive(Debug)]
pub(crate) struct SubmissionPlan {
rate: std::sync::Arc<alpha2_gpu::GpuRate>,
ms_per_unit_pixel: f64,
budget_ms: f64,
predicted_ms: f64,
pass_rates: Vec<(alpha2_gpu::PassKey, f64)>,
}
impl SubmissionPlan {
pub(crate) fn new(
rate: std::sync::Arc<alpha2_gpu::GpuRate>,
ms_per_unit_pixel: f64,
budget_ms: f64,
predicted_ms: f64,
) -> Self {
Self {
rate,
ms_per_unit_pixel,
budget_ms: budget_ms.max(f64::MIN_POSITIVE),
predicted_ms,
pass_rates: Vec::new(),
}
}
pub(crate) fn predict(
&mut self,
passes: impl IntoIterator<Item = (FrameCost, Option<alpha2_gpu::PassKey>)>,
) {
self.predicted_ms = 0.0;
for (work, pass) in passes {
let rate = self.planning_rate(pass);
if let Some(pass) = pass
&& !self.pass_rates.iter().any(|(kept, _)| *kept == pass)
{
self.pass_rates.push((pass, rate));
}
self.predicted_ms += work.unit_pixels() * rate;
}
}
fn planning_rate(&self, pass: Option<alpha2_gpu::PassKey>) -> f64 {
pass.and_then(|pass| {
self.pass_rates
.iter()
.find_map(|(kept, rate)| (*kept == pass).then_some(*rate))
})
.unwrap_or_else(|| self.rate.planning_rate(self.ms_per_unit_pixel, pass))
}
}
#[derive(Debug, Default)]
pub(crate) struct SubmissionPlanner {
plan: Option<SubmissionPlan>,
current_ms: f64,
finished_ms: Vec<f64>,
current_effects: Vec<String>,
finished_effects: Vec<Vec<String>>,
placed_effect: Option<String>,
band_ms: f64,
cost: FrameCost,
placed_ms: f64,
device_rated_ms: f64,
frame: bool,
pilot_rows: Option<u32>,
}
impl SubmissionPlanner {
pub(crate) fn plan(&mut self, plan: SubmissionPlan) {
self.plan = Some(plan);
}
pub(crate) const fn mark_frame(&mut self) {
self.frame = true;
}
pub(crate) const fn mark_pilot(&mut self, rows: u32) {
self.pilot_rows = Some(rows);
}
pub(crate) fn pass(
&mut self,
work: FrameCost,
region: PixelRect,
bandable: bool,
pass: Option<alpha2_gpu::PassKey>,
) -> PassPlacement {
self.cost.add_cost(work);
if let (Some(rows), true) = (self.pilot_rows, bandable) {
return PassPlacement {
cut_before: false,
pieces: Pieces::ONE,
pilot: Some(rows),
};
}
let Some(plan) = &self.plan else {
return PassPlacement {
cut_before: false,
pieces: Pieces::ONE,
pilot: None,
};
};
let rate = plan.planning_rate(pass);
let ms = work.unit_pixels() * rate;
if rate <= plan.ms_per_unit_pixel {
self.device_rated_ms += ms;
}
let unplaced = plan.predicted_ms - self.placed_ms;
let spread = if ms <= unplaced {
unplaced / f64::from(self.spread_buffers_left())
} else {
ms / f64::from(self.quarter_buffers_left())
};
let budget = if spread.is_finite() {
plan.budget_ms.max(spread)
} else {
plan.budget_ms
};
self.placed_ms += ms;
self.place(ms, budget, bandable.then_some(region))
}
fn spread_buffers_left(&self) -> u32 {
(self.buffers_unstarted() * SPREAD_SUBMISSIONS / MAX_FRAME_SUBMISSIONS).max(1)
}
fn quarter_buffers_left(&self) -> u32 {
(self.buffers_unstarted() / 4).max(1)
}
fn buffers_unstarted(&self) -> u32 {
let finished = u32::try_from(self.finished_ms.len()).unwrap_or(u32::MAX);
MAX_FRAME_SUBMISSIONS.saturating_sub(finished)
}
fn place(&mut self, ms: f64, budget: f64, region: Option<PixelRect>) -> PassPlacement {
if ms > budget {
let cut_before = self.current_ms > 0.0 && self.buffers_left() > 0;
if cut_before {
self.finish_buffer();
}
let pieces = region.map_or(Pieces::ONE, |region| {
Pieces::within(ms / budget, region).at_most(self.buffers_left() + 1)
});
let shared = if cut_before { 0.0 } else { self.current_ms };
self.band_ms = ms / f64::from(pieces.count());
self.current_ms = shared + self.band_ms;
return PassPlacement {
cut_before,
pieces,
pilot: None,
};
}
let cut_before =
self.current_ms > 0.0 && self.current_ms + ms > budget && self.buffers_left() > 0;
if cut_before {
self.finish_buffer();
self.current_ms = 0.0;
}
self.current_ms += ms;
PassPlacement {
cut_before,
pieces: Pieces::ONE,
pilot: None,
}
}
fn buffers_left(&self) -> u32 {
let started = u32::try_from(self.finished_ms.len() + 1).unwrap_or(u32::MAX);
MAX_FRAME_SUBMISSIONS.saturating_sub(started)
}
pub(crate) fn next_band(&mut self) {
self.finish_buffer();
self.current_ms = self.band_ms;
self.name_current_buffer();
}
fn finish_buffer(&mut self) {
self.finished_ms.push(self.current_ms);
self.finished_effects
.push(std::mem::take(&mut self.current_effects));
}
pub(crate) fn name_placed(&mut self, effect: Option<&crate::EffectId>) {
let unchanged = match (&self.placed_effect, effect) {
(Some(name), Some(effect)) => names(name, effect),
(None, None) => true,
_ => false,
};
if !unchanged {
self.placed_effect =
effect.map(|effect| format!("{}@{}", effect.as_str(), effect.version()));
}
self.name_current_buffer();
}
fn name_current_buffer(&mut self) {
if let Some(effect) = &self.placed_effect
&& !self.current_effects.contains(effect)
{
self.current_effects.push(effect.clone());
}
}
pub(crate) fn buffer_effects(&self) -> Vec<Vec<String>> {
let mut buffers = self.finished_effects.clone();
buffers.push(self.current_effects.clone());
buffers
}
pub(crate) const fn is_planned(&self) -> bool {
self.plan.is_some()
}
pub(crate) const fn is_frame(&self) -> bool {
self.frame
}
pub(crate) const fn cost(&self) -> FrameCost {
self.cost
}
pub(crate) const fn device_rated_ms(&self) -> f64 {
self.device_rated_ms
}
pub(crate) fn planned_at(&self) -> Option<(f64, f64)> {
self.plan
.as_ref()
.map(|plan| (plan.ms_per_unit_pixel, plan.budget_ms))
}
pub(crate) fn buffer_ms(&self) -> Option<Vec<f64>> {
self.plan.as_ref().map(|_| {
let mut buffers = self.finished_ms.clone();
buffers.push(self.current_ms);
buffers
})
}
}
const CALIBRATION_SIDE: u32 = 512;
const CALIBRATION_BODY: &str = "\
fn effect(uv: vec2f, src: vec4f) -> vec4f {
var acc = src;
for (var i = 0u; i < 64u; i++) {
let s = sample_src(fract(uv + vec2f(f32(i) * 0.0019, f32(i) * 0.0007)));
acc = vec4f(sin(acc.r * 1.37 + s.g), acc.g, acc.b, acc.a);
}
return acc;
}
";
fn calibration_definition() -> crate::EffectDef {
crate::EffectDef {
id: "alpha2.internal.calibrate".into(),
version: 1,
kind: crate::EffectKind::Effect,
inputs: Vec::new(),
params: Vec::new(),
resources: Vec::new(),
wgsl_body: CALIBRATION_BODY.into(),
tier_min: alpha2_gpu::CapabilityTier::TierB,
license: None,
}
}
const CALIBRATION_DRAWS: u32 = 4;
impl crate::Renderer {
pub(crate) fn plan_submissions(&self, cost: FrameCost) -> Option<SubmissionPlan> {
if cost.unit_pixels() <= PLANNING_THRESHOLD_UNIT_PIXELS {
return None;
}
let rate = self.gpu.rate();
if rate.begin_calibration() && self.calibrate().is_err() {
rate.abandon_calibration();
}
let ms_per_unit_pixel = rate
.ms_per_unit_pixel()
.unwrap_or(alpha2_gpu::UNCALIBRATED_MS_PER_UNIT_PIXEL);
Some(SubmissionPlan::new(
std::sync::Arc::clone(rate),
ms_per_unit_pixel,
alpha2_gpu::submission_budget_ms(),
cost.unit_pixels() * ms_per_unit_pixel,
))
}
fn calibrate(&self) -> Result<(), crate::RenderError> {
let effect = self
.calibration
.get_or_init(|| crate::registry::registered_effect(calibration_definition()));
let id = crate::EffectId::new(effect.definition.id.clone(), effect.definition.version);
let size = crate::RenderSize::new(CALIBRATION_SIDE, CALIBRATION_SIDE)?;
let format = self.intermediate_format();
let params = crate::EffectParams::new();
let resources = crate::EffectResources::new();
#[allow(clippy::cast_precision_loss)]
let unit_pixels =
effect.cost.units() as f64 * f64::from(CALIBRATION_SIDE) * f64::from(CALIBRATION_SIDE);
#[cfg(not(target_arch = "wasm32"))]
let mut fastest: Option<f64> = None;
#[cfg(target_arch = "wasm32")]
let mut sequences: Option<std::ops::Range<u64>> = None;
for draw in 0..CALIBRATION_DRAWS {
let mut encoder = self.command_encoder("alpha2 calibration encoder");
let source =
self.textures
.acquire(self.gpu.device(), size, format, "alpha2 calibration source");
let target =
self.textures
.acquire(self.gpu.device(), size, format, "alpha2 calibration target");
let mut uniforms = self.uniform_buffers.acquire(self.gpu.device(), 4096);
self.encode_registered_pass(
&mut encoder,
effect,
crate::runtime_effect::EffectPass {
id: &id,
kind: crate::RenderNodeKind::Effect,
source_a: source.view(),
source_b: None,
inputs: &[],
size,
params: ¶ms,
resources: &resources,
runtime: crate::EffectRuntime::default(),
output: target.view(),
},
&mut uniforms,
)?;
uniforms.flush(self.gpu.queue());
let submitted = self.submit_measured([encoder.finish()], false)?;
uniforms.recycle();
#[cfg(not(target_arch = "wasm32"))]
{
submitted
.submission
.wait(&self.gpu, self.gpu_wait_timeout)?;
if draw > 0
&& let Some(ms) = self.submission_clock.gpu_ms(submitted.sequences.clone())
{
fastest = Some(fastest.map_or(ms, |fastest| fastest.min(ms)));
}
}
#[cfg(target_arch = "wasm32")]
{
let _ = draw;
sequences = Some(sequences.map_or(submitted.sequences.clone(), |sequences| {
sequences.start..submitted.sequences.end
}));
}
}
let rate = std::sync::Arc::clone(self.gpu.rate());
#[cfg(not(target_arch = "wasm32"))]
match fastest {
Some(ms) => rate.seed(ms / unit_pixels),
None => rate.abandon_calibration(),
}
#[cfg(target_arch = "wasm32")]
{
let clock = std::sync::Arc::clone(&self.submission_clock);
let sequences = sequences.expect("calibration draws");
self.gpu
.on_submitted_work_done(move || match clock.gpu_ms(sequences) {
Some(ms) => rate.seed(ms / f64::from(CALIBRATION_DRAWS) / unit_pixels),
None => rate.abandon_calibration(),
});
}
Ok(())
}
}
fn names(name: &str, effect: &crate::EffectId) -> bool {
name.strip_prefix(effect.as_str())
.and_then(|rest| rest.strip_prefix('@'))
.is_some_and(|version| version.parse() == Ok(effect.version()))
}
#[cfg(test)]
mod tests {
use super::*;
const REGION: PixelRect = PixelRect {
x: 0,
y: 0,
width: 512,
height: 288,
};
fn placed(cut_before: bool, rows: u32) -> PassPlacement {
PassPlacement {
cut_before,
pieces: Pieces { rows, columns: 1 },
pilot: None,
}
}
#[test]
fn passes_fill_a_buffer_up_to_the_budget_and_a_heavy_one_is_banded() {
let mut planner = SubmissionPlanner::default();
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 10,
};
assert_eq!(
planner
.pass(FrameCost::of(pass, 100), REGION, true, None)
.pieces,
Pieces::ONE
);
assert_eq!(planner.buffer_ms(), None);
planner.plan(SubmissionPlan::new(
std::sync::Arc::default(),
1.0,
2_500.0,
0.0,
));
assert!(
!planner
.pass(FrameCost::of(pass, 100), REGION, true, None)
.cut_before
);
assert!(
!planner
.pass(FrameCost::of(pass, 100), REGION, true, None)
.cut_before
);
assert!(
planner
.pass(FrameCost::of(pass, 100), REGION, true, None)
.cut_before
);
let placement = planner.pass(FrameCost::of(pass, 1_000), REGION, true, None);
assert_eq!(placement, placed(true, 4));
for _ in 1..placement.pieces.count() {
planner.next_band();
}
assert_eq!(
planner.pass(FrameCost::of(pass, 1_000), REGION, false, None),
placed(true, 1)
);
assert_eq!(
planner.buffer_ms(),
Some(vec![
2_000.0, 1_000.0, 2_500.0, 2_500.0, 2_500.0, 2_500.0, 10_000.0
])
);
assert!((planner.cost().unit_pixels() - 24_000.0).abs() < 1e-9);
assert_eq!(planner.planned_at(), Some((1.0, 2_500.0)));
}
#[test]
fn an_effect_placed_again_after_a_cut_names_the_new_buffer_once() {
let mut planner = SubmissionPlanner::default();
planner.plan(SubmissionPlan::new(
std::sync::Arc::default(),
1.0,
2_500.0,
0.0,
));
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 10,
};
let blur = crate::EffectId::from_static("blur", 2);
let _ = planner.pass(FrameCost::of(pass, 100), REGION, false, None);
planner.name_placed(Some(&blur));
let _ = planner.pass(FrameCost::of(pass, 100), REGION, false, None);
planner.name_placed(Some(&blur));
let _ = planner.pass(FrameCost::of(pass, 10_000), REGION, false, None);
planner.name_placed(Some(&blur));
let buffers = planner.buffer_effects();
assert!(buffers.len() > 1, "{buffers:?}");
for buffer in buffers {
assert_eq!(buffer, ["blur@2"]);
}
}
#[test]
fn effect_names_follow_the_command_buffer_their_pass_is_encoded_into() {
let mut planner = SubmissionPlanner::default();
planner.plan(SubmissionPlan::new(
std::sync::Arc::default(),
1.0,
2_500.0,
0.0,
));
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 10,
};
let blur = crate::EffectId::from_static("blur", 2);
let grain = crate::EffectId::from_static("grain", 1);
let _ = planner.pass(FrameCost::of(pass, 100), REGION, true, None);
planner.name_placed(Some(&blur));
let _ = planner.pass(FrameCost::of(pass, 100), REGION, false, None);
planner.name_placed(None);
let placement = planner.pass(FrameCost::of(pass, 1_000), REGION, true, None);
assert_eq!(placement, placed(true, 4));
planner.name_placed(Some(&grain));
for _ in 1..placement.pieces.count() {
planner.next_band();
}
let _ = planner.pass(FrameCost::of(pass, 100), REGION, true, None);
planner.name_placed(Some(&blur));
let names = |names: &[&str]| -> Vec<String> {
names.iter().map(|name| (*name).to_owned()).collect()
};
assert_eq!(
planner.buffer_effects(),
vec![
names(&["blur@2"]),
names(&["grain@1"]),
names(&["grain@1"]),
names(&["grain@1"]),
names(&["grain@1"]),
names(&["blur@2"]),
]
);
}
#[test]
fn a_pass_measured_slower_than_the_device_is_banded_at_its_own_rate() {
let rate = std::sync::Arc::new(alpha2_gpu::GpuRate::default());
rate.seed(0.5);
let scattered = alpha2_gpu::PassKey::new(1, 100);
rate.record_pass_rate(scattered, 5.0);
let mut planner = SubmissionPlanner::default();
planner.plan(SubmissionPlan::new(rate, 1.0, 2_500.0, 0.0));
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 10,
};
assert_eq!(
planner.pass(FrameCost::of(pass, 100), REGION, true, None),
placed(false, 1)
);
assert_eq!(
planner.pass(FrameCost::of(pass, 100), REGION, true, Some(scattered)),
placed(true, 4)
);
}
#[test]
fn a_pilot_draws_its_rows_alone_and_plans_nothing() {
let mut planner = SubmissionPlanner::default();
planner.mark_pilot(6);
let pass = EffectCost::fixed(1, 0);
assert_eq!(
planner.pass(FrameCost::of(pass, 100), REGION, true, None),
PassPlacement {
cut_before: false,
pieces: Pieces::ONE,
pilot: Some(6)
}
);
assert_eq!(
planner.pass(FrameCost::of(pass, 100), REGION, false, None),
placed(false, 1)
);
assert_eq!(planner.buffer_ms(), None);
}
#[test]
fn a_pass_past_the_most_bands_is_cut_into_tiles_no_smaller_than_a_quad() {
let mut planner = SubmissionPlanner::default();
planner.plan(SubmissionPlan::new(
std::sync::Arc::default(),
1.0,
100.0,
8_000.0,
));
let pass = EffectCost::fixed(0, 0).plus(EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 1,
});
let placement = planner.pass(FrameCost::of(pass, 80 * 100), REGION, true, None);
assert_eq!(
placement.pieces,
Pieces {
rows: 40,
columns: 2
}
);
let tiny = PixelRect {
x: 0,
y: 0,
width: 5,
height: 4,
};
let placement = planner.pass(FrameCost::of(pass, 1_000 * 100), tiny, true, None);
assert_eq!(
placement.pieces,
Pieces {
rows: 2,
columns: 3
}
);
}
#[test]
fn a_frame_is_never_cut_into_more_than_its_most_submissions() {
let mut planner = SubmissionPlanner::default();
planner.plan(SubmissionPlan::new(
std::sync::Arc::default(),
1.0,
1.0,
0.0,
));
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 1,
};
let mut buffers = 1;
for _ in 0..60 {
let placement = planner.pass(FrameCost::of(pass, 100), REGION, true, None);
buffers += u32::from(placement.cut_before);
for _ in 1..placement.pieces.count() {
planner.next_band();
buffers += 1;
}
}
let placement = planner.pass(FrameCost::of(pass, 3), REGION, false, None);
assert!(!placement.cut_before, "a full frame starts no buffer");
let ms = planner.buffer_ms().expect("planned");
assert_eq!(buffers, MAX_FRAME_SUBMISSIONS);
assert_eq!(ms.len(), MAX_FRAME_SUBMISSIONS as usize);
assert!((ms.iter().sum::<f64>() - 6_003.0).abs() < 1e-9, "{ms:?}");
}
#[test]
fn a_frame_predicted_past_its_most_submissions_is_cut_into_longer_ones_evenly() {
let mut planner = SubmissionPlanner::default();
let budgets = f64::from(MAX_FRAME_SUBMISSIONS) * 4.0;
planner.plan(SubmissionPlan::new(
std::sync::Arc::default(),
1.0,
1.0,
budgets,
));
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 1,
};
for _ in 0..4 {
let placement = planner.pass(
FrameCost::of(pass, u64::from(MAX_FRAME_SUBMISSIONS)),
REGION,
true,
None,
);
for _ in 1..placement.pieces.count() {
planner.next_band();
}
}
let ms = planner.buffer_ms().expect("planned");
let spread = budgets / f64::from(SPREAD_SUBMISSIONS);
assert!(ms.len() <= MAX_FRAME_SUBMISSIONS as usize, "{ms:?}");
assert!(ms.iter().all(|ms| *ms <= spread * (1.0 + 1e-9)), "{ms:?}");
}
#[test]
fn a_frame_whose_passes_measured_slower_than_the_device_is_spread_by_their_planned_rate() {
use std::sync::Arc;
let rate = Arc::new(alpha2_gpu::GpuRate::default());
let key = alpha2_gpu::PassKey::new(7, 100);
rate.record_pass_rate(key, 25.0);
let pass = FrameCost::of(
EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 1,
},
100,
);
let passes = 64;
let mut plan = SubmissionPlan::new(Arc::clone(&rate), 1.0, 1.0, f64::from(passes) * 100.0);
plan.predict((0..passes).map(|_| (pass, Some(key))));
let planned_ms = plan.predicted_ms;
rate.record_pass_rate(key, 100.0);
let mut planner = SubmissionPlanner::default();
planner.plan(plan);
for _ in 0..passes {
let placement = planner.pass(pass, REGION, true, Some(key));
for _ in 1..placement.pieces.count() {
planner.next_band();
}
}
let ms = planner.buffer_ms().expect("planned");
let spread = planned_ms / f64::from(SPREAD_SUBMISSIONS);
assert!(ms.len() <= MAX_FRAME_SUBMISSIONS as usize, "{}", ms.len());
assert!(ms.iter().all(|ms| *ms <= spread * (1.0 + 1e-9)), "{ms:?}");
}
#[test]
fn pieces_cover_every_pixel_of_their_region_once_on_even_edges() {
for (pieces, region) in [
(Pieces::ONE, (0, 0, 7, 7)),
(
Pieces {
rows: 3,
columns: 1,
},
(0, 0, 512, 288),
),
(
Pieces {
rows: 7,
columns: 1,
},
(0, 0, 64, 101),
),
(
Pieces {
rows: 64,
columns: 1,
},
(0, 0, 8, 4320),
),
(
Pieces {
rows: 4,
columns: 5,
},
(3, 9, 101, 77),
),
] {
let region = PixelRect {
x: region.0,
y: region.1,
width: region.2,
height: region.3,
};
let mut covered = vec![0_u32; (region.width * region.height) as usize];
for index in 0..pieces.count() {
let piece = pieces.rect(index, region);
for (edge, start, end) in [
(piece.x, region.x, region.x + region.width),
(piece.y, region.y, region.y + region.height),
(piece.x + piece.width, region.x, region.x + region.width),
(piece.y + piece.height, region.y, region.y + region.height),
] {
assert!(edge == start || edge == end || edge % 2 == 0, "{piece:?}");
}
for y in piece.y..piece.y + piece.height {
for x in piece.x..piece.x + piece.width {
covered[((y - region.y) * region.width + x - region.x) as usize] += 1;
}
}
}
assert!(covered.iter().all(|count| *count == 1), "{pieces:?}");
}
}
}