klyff 0.1.2

Text rendering library for games with MSDF support
Documentation
mod material_encoder;
mod mesh_encoder;
mod pipeline;
mod screen_size_uniform;

pub use material_encoder::{GlyphMaterial, MaterialEncoder};
pub use mesh_encoder::MeshEncoder;
pub use pipeline::{MsdfTextPipeline, RasterizedTextPipeline};
pub use screen_size_uniform::ScreenSizeUniform;

use wgpu::util::DeviceExt as _;

use crate::TextureAtlas;

pub use crate::DecodedGlyph;

/// Resources borrowed during encoding.
///
/// This struct groups related dependencies that are commonly consumed by different functions and
/// methods.
pub struct EncoderContext<'a> {
    pub atlas: &'a mut TextureAtlas,
    pub device: &'a wgpu::Device,
    pub queue: &'a wgpu::Queue,
    pub cmd_encoder: &'a mut wgpu::CommandEncoder,
    pub font_system: &'a mut cosmic_text::FontSystem,
}

const DEFAULT_BUFFER_SIZE: u64 = 1024;

/// Uploads `data` into `buffer`, growing the buffer (and returning `true`) if needed.
fn upload_buf(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    buffer: &mut wgpu::Buffer,
    data: &[u8],
    usage: wgpu::BufferUsages,
    label: &str,
) -> bool {
    if data.is_empty() {
        return false;
    }
    let padded_len = data
        .len()
        .next_multiple_of(wgpu::COPY_BUFFER_ALIGNMENT as usize);

    if buffer.size() >= padded_len as u64 {
        queue.write_buffer(buffer, 0, data);
        return false;
    }

    *buffer = if padded_len == data.len() {
        device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some(label),
            contents: data,
            usage,
        })
    } else {
        let mut padded = data.to_vec();
        padded.resize(padded_len, 0u8);
        device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some(label),
            contents: &padded,
            usage,
        })
    };
    true
}