Cleanup of UV space handling
This commit is contained in:
1 parent
8d17f8e64f
commit
7b925876a5
9 files changed
+73
-118
No files matched your search
+1
-82
@@ -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<T> = Point<T, LogicalScreenSpace>;
|
||||
pub type LogicalVector<T> = Vector<T, LogicalScreenSpace>;
|
||||
@@ -19,80 +15,3 @@ pub type PhysicalSize<T> = Size<T, PhysicalScreenSpace>;
|
||||
pub type PhysicalRect<T> = Rect<T, PhysicalScreenSpace>;
|
||||
|
||||
pub type ScaleFactor<F> = Scale<F, LogicalScreenSpace, PhysicalScreenSpace>;
|
||||
|
||||
pub type NormalizedPoint = Point<f32, Normalized>;
|
||||
pub type NormalizedRect = Rect<f32, Normalized>;
|
||||
pub type NormalizedQuad = Quad<f32, Normalized>;
|
||||
|
||||
pub trait Normalize<T, U> {
|
||||
type Output<F>;
|
||||
fn normalize<F: Float>(&self, size: Size<T, U>) -> Self::Output<F>;
|
||||
}
|
||||
|
||||
impl<T, U> Normalize<T, U> for Point<T, U>
|
||||
where
|
||||
T: Copy + std::ops::Div<Output = T> + ToPrimitive,
|
||||
{
|
||||
type Output<V> = Point<V, Normalized>;
|
||||
|
||||
#[inline]
|
||||
fn normalize<F: Float>(&self, size: Size<T, U>) -> Self::Output<F> {
|
||||
Point::new(
|
||||
F::from(self.x).unwrap() / F::from(size.width).unwrap(),
|
||||
F::from(self.y).unwrap() / F::from(size.height).unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> Normalize<T, U> for Rect<T, U>
|
||||
where
|
||||
T: Copy + std::ops::Div<Output = T> + ToPrimitive,
|
||||
{
|
||||
type Output<V> = Rect<V, Normalized>;
|
||||
|
||||
#[inline]
|
||||
fn normalize<F: Float>(&self, size: Size<T, U>) -> Self::Output<F> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
+12
-15
@@ -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<f32>,
|
||||
pub uvs: NormalizedQuad,
|
||||
pub uvs: UvQuad,
|
||||
pub texture_id: TextureId,
|
||||
pub clip_rect: LogicalRect<f32>,
|
||||
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<f32>,
|
||||
texture_id: Option<TextureId>,
|
||||
uv: Option<NormalizedRect>,
|
||||
uv: Option<UvRect>,
|
||||
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<f32>,
|
||||
texture_id: Option<TextureId>,
|
||||
uv: Option<NormalizedQuad>,
|
||||
uv: Option<UvQuad>,
|
||||
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<f32>,
|
||||
uv: NormalizedRect,
|
||||
uv: UvRect,
|
||||
color: Srgba,
|
||||
layer_idx: u32,
|
||||
is_absolute_position: bool,
|
||||
|
||||
+11
-7
@@ -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<u32, FontAtlasSpace>;
|
||||
pub type FontAtlasSize = Size<u32, FontAtlasSpace>;
|
||||
pub type FontAtlasRect = Rect<u32, FontAtlasSpace>;
|
||||
|
||||
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<f32, FontAtlasSpace, UvSpace>,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -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<f32>,
|
||||
pub uv: NormalizedRect,
|
||||
pub uv: UvRect,
|
||||
pub layer_idx: u32,
|
||||
pub color: Srgba,
|
||||
}
|
||||
|
||||
@@ -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<u32>,
|
||||
pub uv: NormalizedRect,
|
||||
pub uv: UvRect,
|
||||
pub texture_layer_idx: u32,
|
||||
// offset from top/left to baseline
|
||||
pub baseline_offset: PhysicalVector<i32>,
|
||||
@@ -61,7 +59,7 @@ pub struct CachedGlyph {
|
||||
pub struct RasterizedGlyph {
|
||||
// NOTE: this is relative to the layout origin
|
||||
pub rect: LogicalRect<f32>,
|
||||
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<u32>, NormalizedRect, u32) {
|
||||
) -> (PhysicalRect<u32>, UvRect, u32) {
|
||||
let mut allocated_glyph =
|
||||
atlas.allocate(Size::new(image.placement.width, image.placement.height));
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ mod pob;
|
||||
mod renderer;
|
||||
mod stage;
|
||||
mod util;
|
||||
mod uv;
|
||||
mod window;
|
||||
mod worker_pool;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
+3
-5
@@ -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<f32>,
|
||||
pub uv: NormalizedPoint,
|
||||
pub uv: UvPoint,
|
||||
pub color: Srgba,
|
||||
/// Index into batch's texture array
|
||||
pub texture_slot: u32,
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
use crate::math::{Point, Quad, Rect, Size};
|
||||
|
||||
pub struct UvSpace;
|
||||
|
||||
pub type UvPoint = Point<f32, UvSpace>;
|
||||
pub type UvRect = Rect<f32, UvSpace>;
|
||||
pub type UvQuad = Quad<f32, UvSpace>;
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user