Files
rusty-path-of-building/src/draw_commands.rs
T
Dodomew 3e065a8838 Snap text to pixel grid after applying viewport offset
Text inside viewports was blurry with fractional scale factors because it was aligned to the pixel grid before the viewport offset was applied.
2026-11-10 22:33:55 +01:00

184 lines
5.0 KiB
Rust

use std::{collections::BTreeMap, hash::Hasher};
use ahash::AHasher;
use crate::{
color::Srgba,
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: UvQuad,
pub texture_id: TextureId,
pub clip_rect: LogicalRect<f32>,
pub texture_layer_idx: u32,
pub color: Srgba,
}
impl DrawCommand {
#[inline(always)]
fn hash_into<H: Hasher>(&self, state: &mut H) {
state.write(bytemuck::bytes_of(self));
}
}
#[derive(Default)]
pub struct DrawCommandRecorder {
layers: BTreeMap<(i32, i32), Vec<DrawCommand>>,
current_layer: (i32, i32),
current_viewport: LogicalRect<f32>,
current_draw_color: Srgba,
hasher: AHasher,
}
impl DrawCommandRecorder {
fn push(&mut self, cmd: DrawCommand) {
cmd.hash_into(&mut self.hasher);
self.layers.entry(self.current_layer).or_default().push(cmd);
}
pub fn reset(&mut self) {
for layer in self.layers.values_mut() {
layer.clear();
}
self.current_layer = (0, 0);
self.current_viewport = LogicalRect::from_size(Size::new(f32::INFINITY, f32::INFINITY));
self.current_draw_color = Srgba::WHITE;
self.hasher = AHasher::default();
}
pub fn set_viewport(&mut self, viewport: LogicalRect<f32>) {
self.current_viewport = viewport;
}
pub fn get_viewport(&self) -> LogicalRect<f32> {
self.current_viewport
}
pub fn set_viewport_from_size(&mut self, size: LogicalSize<f32>) {
self.set_viewport(LogicalRect::from_size(size));
}
pub fn set_draw_layer(&mut self, layer: i32, sublayer: i32) {
self.current_layer = (layer, sublayer);
}
pub fn set_draw_sublayer(&mut self, sublayer: i32) {
self.set_draw_layer(self.current_layer.0, sublayer);
}
pub fn get_draw_layer(&self) -> (i32, i32) {
self.current_layer
}
pub fn set_draw_color(&mut self, color: Srgba) {
self.current_draw_color = color;
}
pub fn get_draw_color(&self) -> Srgba {
self.current_draw_color
}
pub fn draw_image(
&mut self,
rect: LogicalRect<f32>,
texture_id: Option<TextureId>,
uv: Option<UvRect>,
layer_idx: u32,
) {
let (texture_id, uvs, texture_layer_idx) = match texture_id {
Some(texture_id) => (
texture_id,
uv.map_or_else(UvQuad::full_uv, |uv_rect| uv_rect.into()),
layer_idx,
),
None => (TextureId::default(), UvQuad::white_uv(), 0),
};
let positions = rect.translate(self.current_viewport.min.to_vector());
// cull if outside of viewport
if !positions.intersects(&self.current_viewport) {
return;
}
self.push(DrawCommand {
positions: positions.into(),
uvs,
color: self.current_draw_color,
texture_id,
texture_layer_idx,
clip_rect: self.current_viewport,
});
}
pub fn draw_image_quad(
&mut self,
quad: LogicalQuad<f32>,
texture_id: Option<TextureId>,
uv: Option<UvQuad>,
layer_idx: u32,
) {
let (texture_id, uvs, texture_layer_idx) = match texture_id {
Some(texture_id) => (texture_id, uv.unwrap_or_else(UvQuad::full_uv), layer_idx),
None => (TextureId::default(), UvQuad::white_uv(), 0),
};
let positions = quad.translate(self.current_viewport.min.to_vector());
// cull if outside of viewport
if !positions.aabb_intersects_rect(&self.current_viewport) {
return;
}
self.push(DrawCommand {
positions,
uvs,
color: self.current_draw_color,
texture_id,
texture_layer_idx,
clip_rect: self.current_viewport,
});
}
pub fn draw_glyph(
&mut self,
rect: LogicalRect<f32>,
uv: UvRect,
color: Srgba,
layer_idx: u32,
is_absolute_position: bool,
) {
let positions = if is_absolute_position {
rect
} else {
rect.translate(self.current_viewport.min.to_vector())
};
// cull if outside of viewport
if !positions.intersects(&self.current_viewport) {
return;
}
self.push(DrawCommand {
positions: positions.into(),
uvs: uv.into(),
color,
// font atlas always lives at default texture ID
texture_id: TextureId::default(),
texture_layer_idx: layer_idx,
clip_rect: self.current_viewport,
});
}
pub fn finish(&self) -> (u64, &BTreeMap<(i32, i32), Vec<DrawCommand>>) {
(self.hasher.finish(), &self.layers)
}
}