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 COMPOSITE_LAYER_COST: EffectCost = EffectCost::fixed(4, 0);
pub(crate) const VECTOR_COST: EffectCost = EffectCost::fixed(2, 0);
#[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;
}
}
#[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) bands: u32,
pub(crate) pilot: Option<u32>,
}
#[derive(Debug)]
struct Plan {
rate: std::sync::Arc<alpha2_gpu::GpuRate>,
ms_per_unit_pixel: f64,
budget_ms: f64,
}
#[derive(Debug, Default)]
pub(crate) struct SubmissionPlanner {
plan: Option<Plan>,
current_ms: f64,
finished_ms: Vec<f64>,
band_ms: f64,
cost: FrameCost,
frame: bool,
pilot_rows: Option<u32>,
}
impl SubmissionPlanner {
pub(crate) fn plan(
&mut self,
rate: std::sync::Arc<alpha2_gpu::GpuRate>,
ms_per_unit_pixel: f64,
budget_ms: f64,
) {
self.plan = Some(Plan {
rate,
ms_per_unit_pixel,
budget_ms: budget_ms.max(f64::MIN_POSITIVE),
});
}
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,
cost: EffectCost,
pixels: u64,
bandable: bool,
pass: Option<alpha2_gpu::PassKey>,
) -> PassPlacement {
self.cost.add(cost, pixels);
if let (Some(rows), true) = (self.pilot_rows, bandable) {
return PassPlacement {
cut_before: false,
bands: 1,
pilot: Some(rows),
};
}
let Some(plan) = &self.plan else {
return PassPlacement {
cut_before: false,
bands: 1,
pilot: None,
};
};
let budget = plan.budget_ms;
let rate = plan.rate.planning_rate(plan.ms_per_unit_pixel, pass);
#[allow(clippy::cast_precision_loss)]
let ms = cost.units() as f64 * pixels as f64 * rate;
self.place(ms, budget, bandable)
}
fn place(&mut self, ms: f64, budget: f64, bandable: bool) -> PassPlacement {
if ms > budget {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let bands = if bandable {
((ms / budget).ceil() as u32).clamp(1, MAX_BANDS)
} else {
1
};
let cut_before = self.current_ms > 0.0;
if cut_before {
self.finished_ms.push(self.current_ms);
}
self.band_ms = ms / f64::from(bands);
self.current_ms = self.band_ms;
return PassPlacement {
cut_before,
bands,
pilot: None,
};
}
let cut_before = self.current_ms > 0.0 && self.current_ms + ms > budget;
if cut_before {
self.finished_ms.push(self.current_ms);
self.current_ms = 0.0;
}
self.current_ms += ms;
PassPlacement {
cut_before,
bands: 1,
pilot: None,
}
}
pub(crate) fn next_band(&mut self) {
self.finished_ms.push(self.current_ms);
self.current_ms = self.band_ms;
}
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) 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,
encoder: &mut crate::renderer::RenderEncoder,
cost: FrameCost,
) {
encoder.planner().mark_frame();
if cost.unit_pixels() <= PLANNING_THRESHOLD_UNIT_PIXELS {
return;
}
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);
encoder.planner().plan(
std::sync::Arc::clone(rate),
ms_per_unit_pixel,
f64::from(alpha2_gpu::submission_budget_ms()),
);
}
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(())
}
}
pub(crate) fn band_rows(band: u32, bands: u32, height: u32) -> (u32, u32) {
let edge = |index: u32| -> u32 {
if index >= bands {
return height;
}
let row = u64::from(height) * u64::from(index) / u64::from(bands);
u32::try_from(row).unwrap_or(height) & !1
};
let top = edge(band);
(top, edge(band + 1).saturating_sub(top))
}
#[cfg(test)]
mod tests {
use super::*;
fn placed(cut_before: bool, bands: u32) -> PassPlacement {
PassPlacement {
cut_before,
bands,
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(pass, 100, true, None).bands, 1);
assert_eq!(planner.buffer_ms(), None);
planner.plan(std::sync::Arc::default(), 1.0, 2_500.0);
assert!(!planner.pass(pass, 100, true, None).cut_before);
assert!(!planner.pass(pass, 100, true, None).cut_before);
assert!(planner.pass(pass, 100, true, None).cut_before);
let placement = planner.pass(pass, 1_000, true, None);
assert_eq!(placement, placed(true, 4));
for _ in 1..placement.bands {
planner.next_band();
}
assert_eq!(planner.pass(pass, 1_000, 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 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(rate, 1.0, 2_500.0);
let pass = EffectCost {
fetches: 0,
transcendentals: 0,
arithmetic: 10,
};
assert_eq!(planner.pass(pass, 100, true, None), placed(false, 1));
assert_eq!(
planner.pass(pass, 100, 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(pass, 100, true, None),
PassPlacement {
cut_before: false,
bands: 1,
pilot: Some(6)
}
);
assert_eq!(planner.pass(pass, 100, false, None), placed(false, 1));
assert_eq!(planner.buffer_ms(), None);
}
#[test]
fn bands_cover_every_row_once_on_even_edges() {
for (bands, height) in [(1, 7), (3, 288), (7, 101), (64, 4320)] {
let mut next = 0;
for band in 0..bands {
let (top, rows) = band_rows(band, bands, height);
assert_eq!(top, next);
assert_eq!(top % 2, 0);
next = top + rows;
}
assert_eq!(next, height);
}
}
}