use super::{
Color, GlyphAtlas2d, GlyphError, GlyphRun2d, ImageBatch2d, ImageBatchBudget,
ImageBatchPlacement, ImageBatchUploadReport, ImageSprite2d, LogicalScreenPosition,
LogicalViewportRegion, ScreenClipRect, WgpuRenderer, image,
};
#[cfg(test)]
mod tests;
#[cfg(test)]
pub(super) use tests::verify_glyph_batches;
#[derive(Clone, Copy)]
pub struct GlyphRunPlacement2d<'run> {
run: &'run GlyphRun2d,
placement: ImageBatchPlacement,
clip: Option<ScreenClipRect>,
}
impl<'run> GlyphRunPlacement2d<'run> {
pub const fn new(run: &'run GlyphRun2d, placement: ImageBatchPlacement) -> Self {
Self {
run,
placement,
clip: None,
}
}
pub const fn with_clip(mut self, clip: ScreenClipRect) -> Self {
self.clip = Some(clip);
self
}
pub const fn run(self) -> &'run GlyphRun2d {
self.run
}
pub const fn placement(self) -> ImageBatchPlacement {
self.placement
}
pub const fn clip(self) -> Option<ScreenClipRect> {
self.clip
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GlyphBatchBudget {
max_placements: usize,
max_glyphs: usize,
max_retained_bytes: usize,
}
impl GlyphBatchBudget {
pub const RETAINED_BYTES_PER_GLYPH: usize = ImageBatchBudget::RETAINED_BYTES_PER_SPRITE;
pub const RETAINED_BYTES_PER_PLACEMENT: usize = std::mem::size_of::<GlyphBatchSpan>();
pub fn new(
max_placements: usize,
max_glyphs: usize,
max_retained_bytes: usize,
) -> Result<Self, GlyphError> {
if max_placements == 0
|| max_glyphs == 0
|| max_retained_bytes
< Self::RETAINED_BYTES_PER_GLYPH + Self::RETAINED_BYTES_PER_PLACEMENT
{
return Err(GlyphError::InvalidBudget);
}
Ok(Self {
max_placements,
max_glyphs,
max_retained_bytes,
})
}
pub const fn max_placements(self) -> usize {
self.max_placements
}
pub const fn max_glyphs(self) -> usize {
self.max_glyphs
}
pub const fn max_retained_bytes(self) -> usize {
self.max_retained_bytes
}
}
impl Default for GlyphBatchBudget {
fn default() -> Self {
Self {
max_placements: 16_384,
max_glyphs: 100_000,
max_retained_bytes: 32 * 1024 * 1024,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(in crate::renderer) struct GlyphBatchSpan {
pub(in crate::renderer) first: u32,
pub(in crate::renderer) end: u32,
pub(in crate::renderer) clip: Option<ScreenClipRect>,
}
pub struct GlyphBatch2d {
pub(in crate::renderer) batch: ImageBatch2d,
pub(in crate::renderer) spans: Vec<GlyphBatchSpan>,
placement_count: usize,
budget: GlyphBatchBudget,
}
impl GlyphBatch2d {
pub const fn placement_count(&self) -> usize {
self.placement_count
}
pub fn glyph_count(&self) -> usize {
self.batch.sprite_count()
}
pub fn draw_count(&self) -> usize {
self.spans.len()
}
pub fn capacity(&self) -> usize {
self.batch.capacity()
}
pub const fn budget(&self) -> GlyphBatchBudget {
self.budget
}
pub fn recovery_memory_bytes(&self) -> usize {
self.batch
.recovery_memory_bytes()
.saturating_add(span_bytes(self.spans.capacity()))
}
pub fn gpu_allocation_bytes(&self) -> usize {
self.batch.gpu_allocation_bytes()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GlyphBatchUploadReport {
instances: ImageBatchUploadReport,
placements: usize,
draws: usize,
retained_bytes: usize,
peak_retained_bytes: usize,
}
impl GlyphBatchUploadReport {
pub const fn instances(self) -> ImageBatchUploadReport {
self.instances
}
pub const fn placement_count(self) -> usize {
self.placements
}
pub const fn draw_count(self) -> usize {
self.draws
}
pub const fn retained_bytes(self) -> usize {
self.retained_bytes
}
pub const fn peak_retained_bytes(self) -> usize {
self.peak_retained_bytes
}
}
impl WgpuRenderer {
pub fn create_glyph_batch(
&self,
atlas: &GlyphAtlas2d,
placements: &[GlyphRunPlacement2d<'_>],
budget: GlyphBatchBudget,
) -> Result<GlyphBatch2d, GlyphError> {
self.validate_image(&atlas.image)?;
create_resources(
&self.device,
&self.queue,
std::sync::Arc::clone(&self.renderer_identity),
&atlas.image,
placements,
budget,
)
}
pub fn update_glyph_batch(
&self,
atlas: &GlyphAtlas2d,
batch: &mut GlyphBatch2d,
placements: &[GlyphRunPlacement2d<'_>],
) -> Result<GlyphBatchUploadReport, GlyphError> {
self.validate_image_batch(&atlas.image, &batch.batch)?;
update_resources(&self.device, &self.queue, &atlas.image, batch, placements)
}
pub fn restore_glyph_batch(
&self,
atlas: &GlyphAtlas2d,
source: &GlyphBatch2d,
) -> Result<GlyphBatch2d, GlyphError> {
self.validate_image(&atlas.image)?;
restore_resources(
&self.device,
&self.queue,
std::sync::Arc::clone(&self.renderer_identity),
&atlas.image,
source,
)
}
}
fn restore_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer_identity: std::sync::Arc<()>,
image: &super::Image2d,
source: &GlyphBatch2d,
) -> Result<GlyphBatch2d, GlyphError> {
if !std::sync::Arc::ptr_eq(
&image.recovery_identity,
&source.batch.image_recovery_identity,
) {
return Err(super::ImageError::RecoverySourceMismatch.into());
}
image::preflight_image_batch_capacity(device, source.glyph_count(), source.batch.budget())?;
let mut spans = reserve_spans(source.spans.len())?;
spans.extend_from_slice(&source.spans);
let restored_budget = image_budget(source.budget, spans.capacity())?;
let batch = image::restore_image_batch_resources(
device,
queue,
renderer_identity,
image,
&source.batch,
restored_budget,
)?;
Ok(GlyphBatch2d {
batch,
spans,
placement_count: source.placement_count,
budget: source.budget,
})
}
fn span_bytes(count: usize) -> usize {
count.saturating_mul(std::mem::size_of::<GlyphBatchSpan>())
}
fn reserve_spans(count: usize) -> Result<Vec<GlyphBatchSpan>, GlyphError> {
let mut spans = Vec::new();
spans
.try_reserve_exact(count)
.map_err(|_| GlyphError::AllocationFailed {
requested_bytes: span_bytes(count),
})?;
Ok(spans)
}
fn placed_sprite(
sprite: ImageSprite2d,
placement: ImageBatchPlacement,
) -> Result<ImageSprite2d, GlyphError> {
let sprite = translated_sprite(sprite, placement)?;
if !image::logical_image_region_is_portable(sprite.destination()) {
return Err(GlyphError::InvalidGlyph);
}
Ok(sprite)
}
fn translated_sprite(
sprite: ImageSprite2d,
placement: ImageBatchPlacement,
) -> Result<ImageSprite2d, GlyphError> {
let origin = sprite.destination().origin().to_vec2() + placement.translation().to_vec2();
let destination = LogicalViewportRegion::new(
LogicalScreenPosition::from_vec2(origin),
sprite.destination().viewport(),
)
.map_err(|_| GlyphError::InvalidGlyph)?;
let first = sprite.tint().to_array();
let second = placement.tint().to_array();
Ok(ImageSprite2d::new(
sprite.source(),
destination,
Color::rgba(
first[0] * second[0],
first[1] * second[1],
first[2] * second[2],
first[3] * second[3],
),
)?)
}
fn validate_placements(
image: &super::Image2d,
placements: &[GlyphRunPlacement2d<'_>],
budget: GlyphBatchBudget,
) -> Result<(usize, usize), GlyphError> {
if placements.len() > budget.max_placements {
return Err(GlyphError::EntryBudgetExceeded {
limit: budget.max_placements,
actual: placements.len(),
});
}
let mut glyphs = 0_usize;
let mut spans = 0;
let mut previous_clip = None;
for entry in placements {
image::validate_batch_image(image, &entry.run.batch)?;
glyphs =
glyphs
.checked_add(entry.run.glyph_count())
.ok_or(GlyphError::EntryBudgetExceeded {
limit: budget.max_glyphs,
actual: usize::MAX,
})?;
if glyphs > budget.max_glyphs {
return Err(GlyphError::EntryBudgetExceeded {
limit: budget.max_glyphs,
actual: glyphs,
});
}
if entry.run.glyph_count() != 0 {
if previous_clip != Some(entry.clip) {
spans += 1;
}
previous_clip = Some(entry.clip);
}
}
Ok((glyphs, spans))
}
fn image_budget(
budget: GlyphBatchBudget,
span_capacity: usize,
) -> Result<ImageBatchBudget, GlyphError> {
let remaining = budget
.max_retained_bytes
.checked_sub(span_bytes(span_capacity))
.ok_or(GlyphError::MetadataBudgetExceeded {
limit: budget.max_retained_bytes,
actual: span_bytes(span_capacity),
})?;
if remaining < ImageBatchBudget::RETAINED_BYTES_PER_SPRITE {
return Err(GlyphError::MetadataBudgetExceeded {
limit: budget.max_retained_bytes,
actual: span_bytes(span_capacity)
.saturating_add(ImageBatchBudget::RETAINED_BYTES_PER_SPRITE),
});
}
Ok(ImageBatchBudget::new(budget.max_glyphs, remaining)?)
}
fn fill_spans(spans: &mut Vec<GlyphBatchSpan>, placements: &[GlyphRunPlacement2d<'_>]) {
spans.clear();
let mut first = 0_u32;
for entry in placements {
let count = entry.run.glyph_count() as u32;
if count == 0 {
continue;
}
let end = first + count;
if let Some(last) = spans.last_mut()
&& last.clip == entry.clip
{
last.end = end;
} else {
spans.push(GlyphBatchSpan {
first,
end,
clip: entry.clip,
});
}
first = end;
}
}
fn create_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer_identity: std::sync::Arc<()>,
image: &super::Image2d,
placements: &[GlyphRunPlacement2d<'_>],
budget: GlyphBatchBudget,
) -> Result<GlyphBatch2d, GlyphError> {
let (count, span_count) = validate_placements(image, placements, budget)?;
image::preflight_image_batch_capacity(device, count, image_budget(budget, span_count)?)?;
let mut spans = reserve_spans(span_count)?;
let image_budget = image_budget(budget, spans.capacity())?;
image::preflight_image_batch_capacity(device, count, image_budget)?;
let mut sprites = Vec::new();
sprites
.try_reserve_exact(count)
.map_err(|_| GlyphError::AllocationFailed {
requested_bytes: count.saturating_mul(std::mem::size_of::<ImageSprite2d>()),
})?;
for entry in placements {
for sprite in entry.run.batch.sprites() {
sprites.push(placed_sprite(*sprite, entry.placement)?);
}
}
let batch = image::create_image_batch_resources(
device,
queue,
renderer_identity,
image,
sprites,
image_budget,
)?;
fill_spans(&mut spans, placements);
Ok(GlyphBatch2d {
batch,
spans,
placement_count: placements.len(),
budget,
})
}
fn update_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
image: &super::Image2d,
batch: &mut GlyphBatch2d,
placements: &[GlyphRunPlacement2d<'_>],
) -> Result<GlyphBatchUploadReport, GlyphError> {
image::validate_batch_image(image, &batch.batch)?;
let (count, span_count) = validate_placements(image, placements, batch.budget)?;
image::preflight_image_batch_capacity(device, count, image_budget(batch.budget, span_count)?)?;
let old_span_bytes = span_bytes(batch.spans.capacity());
let replacement = (span_count > batch.spans.capacity())
.then(|| reserve_spans(span_count))
.transpose()?;
let span_capacity = replacement
.as_ref()
.map_or(batch.spans.capacity(), Vec::capacity);
let budget = image_budget(batch.budget, span_capacity)?;
let sprites = placements.iter().flat_map(|entry| {
entry
.run
.batch
.sprites()
.iter()
.filter_map(move |sprite| translated_sprite(*sprite, entry.placement).ok())
});
let instances = image::update_image_batch_resources(
device,
queue,
image,
&mut batch.batch,
sprites,
count,
budget,
)
.map_err(|error| match error {
super::ImageError::InvalidSprite => GlyphError::InvalidGlyph,
error => GlyphError::Image(error),
})?;
let peak_retained_bytes = instances
.peak_retained_bytes()
.saturating_add(old_span_bytes)
.saturating_add(if replacement.is_some() {
span_bytes(span_capacity)
} else {
0
});
if let Some(spans) = replacement {
batch.spans = spans;
}
fill_spans(&mut batch.spans, placements);
batch.placement_count = placements.len();
Ok(GlyphBatchUploadReport {
instances,
placements: placements.len(),
draws: batch.spans.len(),
retained_bytes: batch.recovery_memory_bytes(),
peak_retained_bytes,
})
}