-
-
Notifications
You must be signed in to change notification settings - Fork 54
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 a StagingBelt
for regular buffer uploads
#92
Open
hecrj
wants to merge
1
commit into
grovesNL:main
Choose a base branch
from
hecrj:use-staging-belt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,18 @@ 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, | ||
COPY_BUFFER_ALIGNMENT, | ||
util::StagingBelt, Buffer, BufferDescriptor, BufferUsages, CommandEncoder, DepthStencilState, | ||
Device, Extent3d, ImageCopyTexture, ImageDataLayout, MultisampleState, Origin3d, Queue, | ||
RenderPass, RenderPipeline, TextureAspect, COPY_BUFFER_ALIGNMENT, | ||
}; | ||
|
||
/// A text renderer that uses cached glyphs to render text into an existing render pass. | ||
pub struct TextRenderer { | ||
vertex_buffer: Buffer, | ||
vertex_buffer_size: u64, | ||
staging_belt: StagingBelt, | ||
pipeline: Arc<RenderPipeline>, | ||
glyph_vertices: Vec<GlyphToRender>, | ||
} | ||
|
@@ -38,6 +39,7 @@ impl TextRenderer { | |
Self { | ||
vertex_buffer, | ||
vertex_buffer_size, | ||
staging_belt: StagingBelt::new(vertex_buffer_size), | ||
pipeline, | ||
glyph_vertices: Vec::new(), | ||
} | ||
|
@@ -48,6 +50,7 @@ impl TextRenderer { | |
&mut self, | ||
device: &Device, | ||
queue: &Queue, | ||
encoder: &mut CommandEncoder, | ||
font_system: &mut FontSystem, | ||
atlas: &mut TextAtlas, | ||
viewport: &Viewport, | ||
|
@@ -56,6 +59,7 @@ impl TextRenderer { | |
mut metadata_to_depth: impl FnMut(usize) -> f32, | ||
) -> Result<(), PrepareError> { | ||
self.glyph_vertices.clear(); | ||
self.staging_belt.recall(); | ||
|
||
let resolution = viewport.resolution(); | ||
|
||
|
@@ -271,7 +275,16 @@ impl TextRenderer { | |
}; | ||
|
||
if self.vertex_buffer_size >= vertices_raw.len() as u64 { | ||
queue.write_buffer(&self.vertex_buffer, 0, vertices_raw); | ||
self.staging_belt | ||
.write_buffer( | ||
encoder, | ||
&self.vertex_buffer, | ||
0, | ||
NonZeroU64::new(vertices_raw.len() as u64).expect("Non-empty vertices"), | ||
device, | ||
) | ||
.copy_from_slice(vertices_raw); | ||
self.staging_belt.finish(); | ||
} else { | ||
self.vertex_buffer.destroy(); | ||
|
||
|
@@ -284,6 +297,9 @@ impl TextRenderer { | |
|
||
self.vertex_buffer = buffer; | ||
self.vertex_buffer_size = buffer_size; | ||
|
||
self.staging_belt.finish(); | ||
self.staging_belt = StagingBelt::new(buffer_size); | ||
Comment on lines
+301
to
+302
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see, we only need to worry about resizing the belt when the vertex buffer grows, since index buffer uploads will never be bigger than the vertex ones. 6 |
||
} | ||
|
||
Ok(()) | ||
|
@@ -293,6 +309,7 @@ impl TextRenderer { | |
&mut self, | ||
device: &Device, | ||
queue: &Queue, | ||
encoder: &mut CommandEncoder, | ||
font_system: &mut FontSystem, | ||
atlas: &mut TextAtlas, | ||
viewport: &Viewport, | ||
|
@@ -302,6 +319,7 @@ impl TextRenderer { | |
self.prepare_with_depth( | ||
device, | ||
queue, | ||
encoder, | ||
font_system, | ||
atlas, | ||
viewport, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, for lowest memory usage
recall
should be called right after the user submits theQueue
. But this approach removes the burden of aStagingBelt
from the user and keeps it hidden.Worst case scenario, we allocate some additional chunks. Overhead should be constant.