Files
rusty-path-of-building/src/draw_commands.rs
T

197 lines
5.6 KiB
Rust

use std::{collections::BTreeMap, hash::Hasher};
use ahash::AHasher;
use crate::{
color::Srgba,
dpi::{LogicalPoint, LogicalQuad, LogicalRect, LogicalSize, ScaleFactor},
fonts::{Layout, LayoutToLogical},
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 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,
});
}
/// Records a laid-out string whose top-left corner is at `layout_origin`.
pub fn draw_glyphs(
&mut self,
layout: &Layout,
layout_origin: LogicalPoint<f32>,
is_absolute_position: bool,
scale_factor: ScaleFactor<f32>,
) {
// resolve the layout origin to screen space
let origin = if is_absolute_position {
layout_origin
} else {
layout_origin + self.current_viewport.min.to_vector()
};
// align the layout origin with the physical pixel grid to reduce blurriness
let origin = (origin * scale_factor).round() / scale_factor;
for glyph in &layout.glyphs {
let rect = LayoutToLogical::new(origin.x, origin.y).transform_box(&glyph.rect);
self.draw_glyph(rect, glyph.uv, glyph.color, glyph.layer_idx);
}
}
fn draw_glyph(
&mut self,
positions: LogicalRect<f32>,
uv: UvRect,
color: Srgba,
layer_idx: u32,
) {
// 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)
}
}