From 7b925876a5484975ae5fe8bd5b6dff696f0ec3fd Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Tue, 25 Aug 2026 10:46:12 +0200 Subject: [PATCH] Cleanup of UV space handling --- src/dpi.rs | 83 +---------------------------------------- src/draw_commands.rs | 27 ++++++-------- src/fonts/atlas.rs | 18 +++++---- src/fonts/layout.rs | 5 ++- src/fonts/rasterizer.rs | 12 +++--- src/main.rs | 1 + src/math.rs | 1 + src/renderer.rs | 8 ++-- src/uv.rs | 36 ++++++++++++++++++ 9 files changed, 73 insertions(+), 118 deletions(-) create mode 100644 src/uv.rs diff --git a/src/dpi.rs b/src/dpi.rs index 4cf78ff..31a7377 100644 --- a/src/dpi.rs +++ b/src/dpi.rs @@ -1,11 +1,7 @@ -use crate::math::{Point, Quad, Rect, Size, Vector}; -use core::f32; -use euclid::Scale; -use num_traits::{Float, ToPrimitive}; +use crate::math::{Point, Quad, Rect, Scale, Size, Vector}; pub struct LogicalScreenSpace; pub struct PhysicalScreenSpace; -pub struct Normalized; pub type LogicalPoint = Point; pub type LogicalVector = Vector; @@ -19,80 +15,3 @@ pub type PhysicalSize = Size; pub type PhysicalRect = Rect; pub type ScaleFactor = Scale; - -pub type NormalizedPoint = Point; -pub type NormalizedRect = Rect; -pub type NormalizedQuad = Quad; - -pub trait Normalize { - type Output; - fn normalize(&self, size: Size) -> Self::Output; -} - -impl Normalize for Point -where - T: Copy + std::ops::Div + ToPrimitive, -{ - type Output = Point; - - #[inline] - fn normalize(&self, size: Size) -> Self::Output { - Point::new( - F::from(self.x).unwrap() / F::from(size.width).unwrap(), - F::from(self.y).unwrap() / F::from(size.height).unwrap(), - ) - } -} - -impl Normalize for Rect -where - T: Copy + std::ops::Div + ToPrimitive, -{ - type Output = Rect; - - #[inline] - fn normalize(&self, size: Size) -> Self::Output { - Rect::new(self.min.normalize(size), self.max.normalize(size)) - } -} - -pub trait Uv { - fn default_uv() -> Self; - fn white_uv() -> Self; -} - -impl Uv for NormalizedPoint { - #[inline] - fn default_uv() -> Self { - Self::zero() - } - - #[inline] - fn white_uv() -> Self { - Self::zero() - } -} - -impl Uv for NormalizedRect { - #[inline] - fn default_uv() -> Self { - Self::from_size(Size::new(1.0, 1.0)) - } - - #[inline] - fn white_uv() -> Self { - Self::zero() - } -} - -impl Uv for NormalizedQuad { - #[inline] - fn default_uv() -> Self { - Self::from_size(Size::new(1.0, 1.0)) - } - - #[inline] - fn white_uv() -> Self { - Self::zero() - } -} diff --git a/src/draw_commands.rs b/src/draw_commands.rs index 69e7ce5..f05f42b 100644 --- a/src/draw_commands.rs +++ b/src/draw_commands.rs @@ -1,19 +1,20 @@ use std::{collections::BTreeMap, hash::Hasher}; use ahash::AHasher; -use euclid::Size2D; use crate::{ color::Srgba, - dpi::{LogicalQuad, LogicalRect, LogicalSize, NormalizedQuad, NormalizedRect, Uv}, + dpi::{LogicalQuad, LogicalRect, LogicalSize}, + math::Size, renderer::textures::TextureId, + uv::{UvConstructors, UvQuad, UvRect}, }; #[repr(C)] #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] pub struct DrawCommand { pub positions: LogicalQuad, - pub uvs: NormalizedQuad, + pub uvs: UvQuad, pub texture_id: TextureId, pub clip_rect: LogicalRect, pub texture_layer_idx: u32, @@ -47,7 +48,7 @@ impl DrawCommandRecorder { layer.clear(); } self.current_layer = (0, 0); - self.current_viewport = LogicalRect::from_size(Size2D::new(f32::INFINITY, f32::INFINITY)); + self.current_viewport = LogicalRect::from_size(Size::new(f32::INFINITY, f32::INFINITY)); self.current_draw_color = Srgba::WHITE; self.hasher = AHasher::default(); } @@ -80,16 +81,16 @@ impl DrawCommandRecorder { &mut self, rect: LogicalRect, texture_id: Option, - uv: Option, + uv: Option, layer_idx: u32, ) { let (texture_id, uvs, texture_layer_idx) = match texture_id { Some(texture_id) => ( texture_id, - uv.map_or(NormalizedQuad::default_uv(), |uv_rect| uv_rect.into()), + uv.map_or(UvQuad::full_uv(), |uv_rect| uv_rect.into()), layer_idx, ), - None => (TextureId::default(), NormalizedQuad::white_uv(), 0), + None => (TextureId::default(), UvQuad::white_uv(), 0), }; let positions = rect.translate(self.current_viewport.min.to_vector()); @@ -113,16 +114,12 @@ impl DrawCommandRecorder { &mut self, quad: LogicalQuad, texture_id: Option, - uv: Option, + uv: Option, layer_idx: u32, ) { let (texture_id, uvs, texture_layer_idx) = match texture_id { - Some(texture_id) => ( - texture_id, - uv.unwrap_or(NormalizedQuad::default_uv()), - layer_idx, - ), - None => (TextureId::default(), NormalizedQuad::white_uv(), 0), + Some(texture_id) => (texture_id, uv.unwrap_or(UvQuad::full_uv()), layer_idx), + None => (TextureId::default(), UvQuad::white_uv(), 0), }; let positions = quad.translate(self.current_viewport.min.to_vector()); @@ -145,7 +142,7 @@ impl DrawCommandRecorder { pub fn draw_glyph( &mut self, rect: LogicalRect, - uv: NormalizedRect, + uv: UvRect, color: Srgba, layer_idx: u32, is_absolute_position: bool, diff --git a/src/fonts/atlas.rs b/src/fonts/atlas.rs index 384c6ea..2076ef0 100644 --- a/src/fonts/atlas.rs +++ b/src/fonts/atlas.rs @@ -2,23 +2,24 @@ use std::num::NonZeroU32; use crate::{ color::Srgba, - dpi::{Normalize, NormalizedRect}, - math::{Point, Size}, + math::{Point, Rect, Scale, Size}, renderer::{ image::{DataOrder, ImageData, ImageDelta}, textures::TextureOptions, }, + uv::{UvRect, UvSpace}, }; use image::{GenericImage, RgbaImage, SubImage}; pub struct FontAtlasSpace; pub type FontAtlasPoint = Point; pub type FontAtlasSize = Size; +pub type FontAtlasRect = Rect; pub struct AllocatedGlyph<'a> { pub sub_image: SubImage<&'a mut RgbaImage>, pub layer_idx: u32, - pub uv: NormalizedRect, + pub uv: UvRect, } struct Layer { @@ -36,6 +37,8 @@ pub struct FontAtlas { // atlas has been altered and needs to be reuploaded to the GPU // TODO: only mark changed regions as dirty and perform partial texture update dirty: bool, + // used to convert to normalized UV coordinates + to_uv: Scale, } impl FontAtlas { @@ -45,6 +48,7 @@ impl FontAtlas { max_layers, layers: Vec::new(), dirty: false, + to_uv: Scale::new(1.0 / layer_size as f32), }; atlas.push_layer(); @@ -105,6 +109,9 @@ impl FontAtlas { layer.cursor.x += requested_size.width + PADDING; self.dirty = true; + let glyph_rect = FontAtlasRect::from_origin_and_size(pos, requested_size); + let uv = self.to_uv.transform_box2d(&glyph_rect.cast()); + AllocatedGlyph { sub_image: layer.image.sub_image( pos.x, @@ -113,10 +120,7 @@ impl FontAtlas { requested_size.height, ), layer_idx: idx as u32, - uv: NormalizedRect::new( - pos.normalize(self.layer_size), - (pos + requested_size.to_vector()).normalize(self.layer_size), - ), + uv, } } diff --git a/src/fonts/layout.rs b/src/fonts/layout.rs index 0e6ce14..e168446 100644 --- a/src/fonts/layout.rs +++ b/src/fonts/layout.rs @@ -1,6 +1,7 @@ use crate::{ color::Srgba, - dpi::{LogicalPoint, LogicalRect, NormalizedRect}, + dpi::{LogicalPoint, LogicalRect}, + uv::UvRect, }; use ordered_float::OrderedFloat; use parley::{FontFamily, FontFamilyName}; @@ -106,7 +107,7 @@ pub struct PositionedGlyph { // relative to layout origin // TODO: introduce new "layout space"? pub rect: LogicalRect, - pub uv: NormalizedRect, + pub uv: UvRect, pub layer_idx: u32, pub color: Srgba, } diff --git a/src/fonts/rasterizer.rs b/src/fonts/rasterizer.rs index d44a249..b50f170 100644 --- a/src/fonts/rasterizer.rs +++ b/src/fonts/rasterizer.rs @@ -1,11 +1,9 @@ use crate::{ color::Srgba, - dpi::{ - LogicalRect, LogicalVector, NormalizedRect, PhysicalPoint, PhysicalRect, PhysicalVector, - ScaleFactor, - }, + dpi::{LogicalRect, LogicalVector, PhysicalPoint, PhysicalRect, PhysicalVector, ScaleFactor}, fonts::{atlas::FontAtlas, glyph_key::GlyphKey}, math::Size, + uv::UvRect, }; use ahash::HashMap; use image::GenericImage; @@ -52,7 +50,7 @@ impl<'a> StyleKey<'a> { #[derive(Clone, Copy, Debug, PartialEq)] pub struct CachedGlyph { pub rect: PhysicalRect, - pub uv: NormalizedRect, + pub uv: UvRect, pub texture_layer_idx: u32, // offset from top/left to baseline pub baseline_offset: PhysicalVector, @@ -61,7 +59,7 @@ pub struct CachedGlyph { pub struct RasterizedGlyph { // NOTE: this is relative to the layout origin pub rect: LogicalRect, - pub uv: NormalizedRect, + pub uv: UvRect, pub texture_layer_idx: u32, pub color: Srgba, } @@ -244,7 +242,7 @@ impl GlyphRasterizer { fn write_to_atlas( image: &swash::scale::image::Image, atlas: &mut FontAtlas, -) -> (PhysicalRect, NormalizedRect, u32) { +) -> (PhysicalRect, UvRect, u32) { let mut allocated_glyph = atlas.allocate(Size::new(image.placement.width, image.placement.height)); diff --git a/src/main.rs b/src/main.rs index 0b81fe6..5aeb7fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ mod pob; mod renderer; mod stage; mod util; +mod uv; mod window; mod worker_pool; diff --git a/src/math.rs b/src/math.rs index 0ba7ab9..b408f1b 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,5 +1,6 @@ pub use euclid::Box2D as Rect; pub use euclid::Point2D as Point; +pub use euclid::Scale; pub use euclid::Size2D as Size; pub use euclid::Vector2D as Vector; pub use quad::Quad; diff --git a/src/renderer.rs b/src/renderer.rs index dfb122a..aa4b665 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,15 +1,13 @@ use crate::{ color::Srgba, - dpi::{ - LogicalPoint, LogicalRect, LogicalSize, NormalizedPoint, PhysicalRect, PhysicalSize, - ScaleFactor, - }, + dpi::{LogicalPoint, LogicalRect, LogicalSize, PhysicalRect, PhysicalSize, ScaleFactor}, math::Point, renderer::{ image::ImageData, textures::{TextureId, TextureOptions, TexturesDelta}, }, util::calculate_hash, + uv::UvPoint, }; use ahash::HashMap; use std::{ @@ -32,7 +30,7 @@ const DUMMY_TEXTURE_ID: TextureId = 0; #[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)] pub struct Vertex { pub pos: LogicalPoint, - pub uv: NormalizedPoint, + pub uv: UvPoint, pub color: Srgba, /// Index into batch's texture array pub texture_slot: u32, diff --git a/src/uv.rs b/src/uv.rs new file mode 100644 index 0000000..1fcd411 --- /dev/null +++ b/src/uv.rs @@ -0,0 +1,36 @@ +use crate::math::{Point, Quad, Rect, Size}; + +pub struct UvSpace; + +pub type UvPoint = Point; +pub type UvRect = Rect; +pub type UvQuad = Quad; + +pub trait UvConstructors { + fn full_uv() -> Self; + fn white_uv() -> Self; +} + +impl UvConstructors for UvRect { + #[inline] + fn full_uv() -> Self { + Self::from_size(Size::new(1.0, 1.0)) + } + + #[inline] + fn white_uv() -> Self { + Self::zero() + } +} + +impl UvConstructors for UvQuad { + #[inline] + fn full_uv() -> Self { + Self::from_size(Size::new(1.0, 1.0)) + } + + #[inline] + fn white_uv() -> Self { + Self::zero() + } +}