Validate shared screen resolution

This commit is contained in:
grovesNL 2022-05-16 13:23:58 -02:30 committed by Josh Groves
parent 7aa2bd7b72
commit 81671d02b8

View file

@ -58,7 +58,8 @@ impl Error for PrepareError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RenderError { pub enum RenderError {
AtlasFull, RemovedFromAtlas,
ScreenResolutionChanged,
} }
impl Display for RenderError { impl Display for RenderError {
@ -328,6 +329,7 @@ pub struct TextRenderer {
vertices_to_render: u32, vertices_to_render: u32,
atlas: TextAtlas, atlas: TextAtlas,
glyphs_in_use: HashSet<GlyphRasterConfig>, glyphs_in_use: HashSet<GlyphRasterConfig>,
screen_resolution: Resolution,
} }
impl TextRenderer { impl TextRenderer {
@ -356,6 +358,10 @@ impl TextRenderer {
vertices_to_render: 0, vertices_to_render: 0,
atlas: atlas.clone(), atlas: atlas.clone(),
glyphs_in_use: HashSet::new(), glyphs_in_use: HashSet::new(),
screen_resolution: Resolution {
width: 0,
height: 0,
},
} }
} }
@ -367,12 +373,14 @@ impl TextRenderer {
fonts: &[Font], fonts: &[Font],
layouts: &[Layout<impl HasColor>], layouts: &[Layout<impl HasColor>],
) -> Result<(), PrepareError> { ) -> Result<(), PrepareError> {
let current_resolution = { self.screen_resolution = screen_resolution;
let atlas_current_resolution = {
let atlas = self.atlas.inner.read().expect("atlas locked"); let atlas = self.atlas.inner.read().expect("atlas locked");
atlas.params.screen_resolution atlas.params.screen_resolution
}; };
if screen_resolution != current_resolution { if screen_resolution != atlas_current_resolution {
let mut atlas = self.atlas.inner.write().expect("atlas locked"); let mut atlas = self.atlas.inner.write().expect("atlas locked");
atlas.params.screen_resolution = screen_resolution; atlas.params.screen_resolution = screen_resolution;
queue.write_buffer(&atlas.params_buffer, 0, unsafe { queue.write_buffer(&atlas.params_buffer, 0, unsafe {
@ -599,11 +607,19 @@ impl TextRenderer {
return Ok(()); return Ok(());
} }
// Validate that glyphs haven't been evicted from cache since `prepare` {
let atlas = self.atlas.inner.read().expect("atlas locked"); let atlas = self.atlas.inner.read().expect("atlas locked");
for glyph in self.glyphs_in_use.iter() {
if !atlas.glyph_cache.contains_key(glyph) { // Validate that glyphs haven't been evicted from cache since `prepare`
return Err(RenderError::AtlasFull); for glyph in self.glyphs_in_use.iter() {
if !atlas.glyph_cache.contains_key(glyph) {
return Err(RenderError::RemovedFromAtlas);
}
}
// Validate that screen resolution hasn't changed since `prepare`
if self.screen_resolution != atlas.params.screen_resolution {
return Err(RenderError::ScreenResolutionChanged);
} }
} }