-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Texture atlas #154
Merged
+957
−351
Merged
Texture atlas #154
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1bcfc9a
Implemented a texture atlas for images and svgs.
Maldela 743637e
Merged image and svg texture atlases into one owned by the image pipe…
Maldela 82e0675
Some small debug changes
Maldela 8562a4c
Fixed texture bleeding
Maldela 2f77a6b
Use array of atlases instead of one growing indefinitely.
Maldela 3f38835
Implement allocating large images across multiple texture array layers.
Maldela c099692
Batch image draw calls into one with multiple instances
Maldela 2f695ef
Updated shaders and removed debug_stub_derive dependency
Maldela 8f9f44b
When deallocating the last allocation in an allocator mark its layer …
Maldela 4617da2
Implemented automatic deallocation of texture space for dropped alloc…
Maldela 82f0a49
Recompile `image` shaders
hecrj 59d45a5
Refactor texture atlas
hecrj c58d94f
Avoid creating a vertex buffer every frame
hecrj 48d7028
Fix multiple issues from the refactoring
hecrj d06d06e
Deallocate atlas entries and remove padding
hecrj 883a9f2
Add `env_logger` to `svg` example
hecrj 6cb7fb6
Remove unused code warnings in `iced_wgpu::image`
hecrj deedf6e
Make new `texture` module private for now
hecrj 271725f
Derive `Debug` for `raster::Memory`
hecrj bb397cc
Move `Debug` implementation for `vector::Svg`
hecrj fc55e3a
Move `Atlas::deallocate` after `allocate`
hecrj 4e7159c
Stop creating image pipeline when unnecessary
hecrj 88d4cd0
Remove unnecessary `pub(crate) use`
hecrj 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
Fix multiple issues from the refactoring
- Update texture view on grow - Fix atlas texture coordinates - Fix fragmented uploads
- winit-0.3.0
- winit-0.2.0
- winit-0.1.0
- wgpu-0.4.0
- wgpu-0.3.0
- wgpu-0.2.3
- wgpu-0.2.2
- wgpu-0.2.0
- web-0.4.0
- web-0.3.0
- web-0.2.0
- text-editor
- style-0.3.0
- style-0.2.0
- style-0.1.0
- native-0.4.0
- native-0.3.0
- native-0.2.2
- native-0.2.0
- graphics-0.2.0
- graphics-0.1.0
- glutin-0.2.0
- glutin-0.1.0
- glow-0.2.0
- glow-0.1.0
- futures-0.3.0
- futures-0.2.0
- futures-0.1.2
- futures-0.1.0
- core-0.4.0
- core-0.3.0
- core-0.2.0
- 0.13.1
- 0.13.0
- 0.12.1
- 0.12.0
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.1
- 0.1.0
commit 48d70280eb4f5908f1c9339bebdfbab856d55ae1
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
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ pub use layer::Layer; | |
|
||
use allocator::Allocator; | ||
|
||
pub const SIZE: u32 = 4096; | ||
pub const SIZE: u32 = 2048; | ||
|
||
#[derive(Debug)] | ||
pub struct Atlas { | ||
|
@@ -78,31 +78,42 @@ impl Atlas { | |
entry | ||
}; | ||
|
||
log::info!("Allocated atlas entry: {:?}", entry); | ||
|
||
let buffer = device | ||
.create_buffer_mapped(data.len(), wgpu::BufferUsage::COPY_SRC) | ||
.fill_from_slice(data); | ||
|
||
match &entry { | ||
Entry::Contiguous(allocation) => { | ||
self.upload_texture(&buffer, 0, &allocation, encoder); | ||
self.upload_allocation( | ||
&buffer, | ||
width, | ||
height, | ||
0, | ||
&allocation, | ||
encoder, | ||
); | ||
} | ||
Entry::Fragmented { fragments, .. } => { | ||
for fragment in fragments { | ||
let (x, y) = fragment.allocation.position(); | ||
|
||
let offset = | ||
(y * height + x) as usize * std::mem::size_of::<C>(); | ||
let (x, y) = fragment.position; | ||
let offset = (y * width + x) as usize * 4; | ||
|
||
self.upload_texture( | ||
self.upload_allocation( | ||
&buffer, | ||
offset as u64, | ||
width, | ||
height, | ||
offset, | ||
&fragment.allocation, | ||
encoder, | ||
); | ||
} | ||
} | ||
} | ||
|
||
log::info!("Current atlas: {:?}", &self); | ||
|
||
Some(entry) | ||
} | ||
|
||
|
@@ -204,10 +215,12 @@ impl Atlas { | |
None | ||
} | ||
|
||
fn upload_texture( | ||
fn upload_allocation( | ||
&mut self, | ||
buffer: &wgpu::Buffer, | ||
offset: u64, | ||
image_width: u32, | ||
image_height: u32, | ||
offset: usize, | ||
allocation: &Allocation, | ||
encoder: &mut wgpu::CommandEncoder, | ||
) { | ||
|
@@ -224,9 +237,9 @@ impl Atlas { | |
encoder.copy_buffer_to_texture( | ||
wgpu::BufferCopyView { | ||
buffer, | ||
offset, | ||
row_pitch: 4 * width, | ||
image_height: height, | ||
offset: offset as u64, | ||
row_pitch: 4 * image_width, | ||
image_height, | ||
}, | ||
wgpu::TextureCopyView { | ||
texture: &self.texture, | ||
|
@@ -258,7 +271,7 @@ impl Atlas { | |
height: SIZE, | ||
depth: 1, | ||
}, | ||
array_layer_count: (self.layers.len() + amount) as u32, | ||
array_layer_count: self.layers.len() as u32, | ||
mip_level_count: 1, | ||
sample_count: 1, | ||
dimension: wgpu::TextureDimension::D2, | ||
|
@@ -268,7 +281,11 @@ impl Atlas { | |
| wgpu::TextureUsage::SAMPLED, | ||
}); | ||
|
||
for (i, layer) in self.layers.iter_mut().enumerate() { | ||
let amount_to_copy = self.layers.len() - amount; | ||
|
||
for (i, layer) in | ||
self.layers.iter_mut().take(amount_to_copy).enumerate() | ||
{ | ||
if layer.is_empty() { | ||
continue; | ||
} | ||
|
@@ -302,10 +319,7 @@ impl Atlas { | |
); | ||
} | ||
|
||
for _ in 0..amount { | ||
self.layers.push(Layer::Empty); | ||
} | ||
|
||
self.texture = new_texture; | ||
self.texture_view = self.texture.create_default_view(); | ||
} | ||
} | ||
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. Overall, I really like the simplicity of this module. |
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
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.
I have simplified this a bunch. It turns out you can use
copy_buffer_to_texture
to upload a sub-region of an image quite easily.