Fix race condition when growing an image::Atlas

This commit is contained in:
Héctor Ramón Jiménez 2023-05-11 20:18:36 +02:00
parent cf434236e7
commit f02f0c01ea
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
4 changed files with 30 additions and 19 deletions

View file

@ -369,7 +369,6 @@ impl Pipeline {
layer::Image::Raster { handle, bounds } => { layer::Image::Raster { handle, bounds } => {
if let Some(atlas_entry) = raster_cache.upload( if let Some(atlas_entry) = raster_cache.upload(
device, device,
queue,
encoder, encoder,
handle, handle,
&mut self.texture_atlas, &mut self.texture_atlas,
@ -395,7 +394,6 @@ impl Pipeline {
if let Some(atlas_entry) = vector_cache.upload( if let Some(atlas_entry) = vector_cache.upload(
device, device,
queue,
encoder, encoder,
handle, handle,
*color, *color,

View file

@ -65,7 +65,6 @@ impl Atlas {
pub fn upload( pub fn upload(
&mut self, &mut self,
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
width: u32, width: u32,
height: u32, height: u32,
@ -75,6 +74,9 @@ impl Atlas {
let current_size = self.layers.len(); let current_size = self.layers.len();
let entry = self.allocate(width, height)?; let entry = self.allocate(width, height)?;
dbg!(&entry);
dbg!(&self.layers);
// We grow the internal texture after allocating if necessary // We grow the internal texture after allocating if necessary
let new_layers = self.layers.len() - current_size; let new_layers = self.layers.len() - current_size;
self.grow(new_layers, device, encoder); self.grow(new_layers, device, encoder);
@ -112,7 +114,8 @@ impl Atlas {
padding, padding,
0, 0,
allocation, allocation,
queue, device,
encoder,
); );
} }
Entry::Fragmented { fragments, .. } => { Entry::Fragmented { fragments, .. } => {
@ -127,7 +130,8 @@ impl Atlas {
padding, padding,
offset, offset,
&fragment.allocation, &fragment.allocation,
queue, device,
encoder,
); );
} }
} }
@ -280,8 +284,11 @@ impl Atlas {
padding: u32, padding: u32,
offset: usize, offset: usize,
allocation: &Allocation, allocation: &Allocation,
queue: &wgpu::Queue, device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
) { ) {
use wgpu::util::DeviceExt;
let (x, y) = allocation.position(); let (x, y) = allocation.position();
let Size { width, height } = allocation.size(); let Size { width, height } = allocation.size();
let layer = allocation.layer(); let layer = allocation.layer();
@ -292,7 +299,22 @@ impl Atlas {
depth_or_array_layers: 1, depth_or_array_layers: 1,
}; };
queue.write_texture( let buffer =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("image upload buffer"),
contents: data,
usage: wgpu::BufferUsages::COPY_SRC,
});
encoder.copy_buffer_to_texture(
wgpu::ImageCopyBuffer {
buffer: &buffer,
layout: wgpu::ImageDataLayout {
offset: offset as u64,
bytes_per_row: Some(4 * image_width + padding),
rows_per_image: Some(image_height),
},
},
wgpu::ImageCopyTexture { wgpu::ImageCopyTexture {
texture: &self.texture, texture: &self.texture,
mip_level: 0, mip_level: 0,
@ -303,12 +325,6 @@ impl Atlas {
}, },
aspect: wgpu::TextureAspect::default(), aspect: wgpu::TextureAspect::default(),
}, },
data,
wgpu::ImageDataLayout {
offset: offset as u64,
bytes_per_row: Some(4 * image_width + padding),
rows_per_image: Some(image_height),
},
extent, extent,
); );
} }

View file

@ -63,7 +63,6 @@ impl Cache {
pub fn upload( pub fn upload(
&mut self, &mut self,
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
handle: &image::Handle, handle: &image::Handle,
atlas: &mut Atlas, atlas: &mut Atlas,
@ -73,8 +72,7 @@ impl Cache {
if let Memory::Host(image) = memory { if let Memory::Host(image) = memory {
let (width, height) = image.dimensions(); let (width, height) = image.dimensions();
let entry = let entry = atlas.upload(device, encoder, width, height, image)?;
atlas.upload(device, queue, encoder, width, height, image)?;
*memory = Memory::Device(entry); *memory = Memory::Device(entry);
} }

View file

@ -74,7 +74,6 @@ impl Cache {
pub fn upload( pub fn upload(
&mut self, &mut self,
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
handle: &svg::Handle, handle: &svg::Handle,
color: Option<Color>, color: Option<Color>,
@ -138,8 +137,8 @@ impl Cache {
}); });
} }
let allocation = atlas let allocation =
.upload(device, queue, encoder, width, height, &rgba)?; atlas.upload(device, encoder, width, height, &rgba)?;
log::debug!("allocating {} {}x{}", id, width, height); log::debug!("allocating {} {}x{}", id, width, height);