Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use write_buffer_with for buffer uploads #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/hello-world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use glyphon::{
Attrs, Buffer, Cache, Color, Family, FontSystem, Metrics, Resolution, Shaping, SwashCache,
TextArea, TextAtlas, TextBounds, TextRenderer, Viewport,
};
use std::sync::Arc;
use wgpu::{
CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance,
InstanceDescriptor, Limits, LoadOp, MultisampleState, Operations, PresentMode,
Expand All @@ -15,8 +16,6 @@ use winit::{
window::WindowBuilder,
};

use std::sync::Arc;

fn main() {
pollster::block_on(run());
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod text_atlas;
mod text_render;
mod viewport;

use std::{mem, num::NonZeroU64};

pub use cache::Cache;
pub use error::{PrepareError, RenderError};
pub use text_atlas::{ColorMode, TextAtlas};
Expand Down Expand Up @@ -76,6 +78,10 @@ pub(crate) struct Params {
_pad: [u32; 2],
}

pub(crate) const PARAMS_LEN: usize = mem::size_of::<Params>();
pub(crate) const PARAMS_LEN_NONZERO: NonZeroU64 =
unsafe { NonZeroU64::new_unchecked(PARAMS_LEN as u64) };

/// Controls the visible area of the text. Any text outside of the visible area will be clipped.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TextBounds {
Expand Down
15 changes: 11 additions & 4 deletions src/text_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, PrepareError, RenderError,
SwashCache, SwashContent, TextArea, TextAtlas, Viewport,
};
use std::{slice, sync::Arc};
use std::{num::NonZeroU64, slice, sync::Arc};
use wgpu::{
Buffer, BufferDescriptor, BufferUsages, DepthStencilState, Device, Extent3d, ImageCopyTexture,
ImageDataLayout, MultisampleState, Origin3d, Queue, RenderPass, RenderPipeline, TextureAspect,
Expand Down Expand Up @@ -269,9 +269,16 @@ impl TextRenderer {
std::mem::size_of_val(vertices),
)
};

if self.vertex_buffer_size >= vertices_raw.len() as u64 {
queue.write_buffer(&self.vertex_buffer, 0, vertices_raw);
let vertices_size = vertices_raw.len() as u64;

if self.vertex_buffer_size >= vertices_size {
if let Some(mut view) = queue.write_buffer_with(
&self.vertex_buffer,
0,
NonZeroU64::new(vertices_size).expect("buffer size of 0"),
) {
view.copy_from_slice(vertices_raw);
}
} else {
self.vertex_buffer.destroy();

Expand Down
29 changes: 15 additions & 14 deletions src/viewport.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{Cache, Params, Resolution};

use crate::{Cache, Params, Resolution, PARAMS_LEN, PARAMS_LEN_NONZERO};
use std::{mem, slice};
use wgpu::{BindGroup, Buffer, BufferDescriptor, BufferUsages, Device, Queue};

use std::mem;
use std::slice;

#[derive(Debug)]
pub struct Viewport {
params: Params,
Expand Down Expand Up @@ -39,15 +36,19 @@ impl Viewport {
}

pub fn update(&mut self, queue: &Queue, resolution: Resolution) {
if self.params.screen_resolution != resolution {
self.params.screen_resolution = resolution;

queue.write_buffer(&self.params_buffer, 0, unsafe {
slice::from_raw_parts(
&self.params as *const Params as *const u8,
mem::size_of::<Params>(),
)
});
if self.params.screen_resolution == resolution {
return;
}

self.params.screen_resolution = resolution;

if let Some(mut view) = queue.write_buffer_with(&self.params_buffer, 0, PARAMS_LEN_NONZERO)
{
let params_raw = unsafe {
slice::from_raw_parts(&self.params as *const Params as *const u8, PARAMS_LEN)
};

view.copy_from_slice(params_raw);
}
}

Expand Down
Loading