Skip to content

Tolerate destruction of textures used in immediate queue operations prior to submit #7843

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

Open
wants to merge 2 commits into
base: trunk
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cts_runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ description = "CTS runner for wgpu"
license.workspace = true
publish = false

[dependencies]
env_logger.workspace = true

# We make all dependencies conditional on not being wasm,
# so the whole workspace can built as wasm.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
1 change: 1 addition & 0 deletions cts_runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,6 @@ impl deno_web::TimersPermission for Permissions {

#[tokio::main(flavor = "current_thread")]
async fn main() {
env_logger::init();
unwrap_or_exit(run().await)
}
1 change: 1 addition & 0 deletions cts_runner/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ webgpu:api,operation,compute,basic:memcpy:*
//FAIL: webgpu:api,operation,compute,basic:large_dispatch:*
webgpu:api,operation,compute_pipeline,overrides:*
webgpu:api,operation,device,lost:*
webgpu:api,validation,image_copy,texture_related:format:dimension="1d";*
webgpu:api,validation,queue,submit:command_buffer,device_mismatch:*
webgpu:api,validation,queue,submit:command_buffer,duplicate_buffers:*
webgpu:api,validation,queue,submit:command_buffer,submit_invalidates:*
Expand Down
51 changes: 51 additions & 0 deletions tests/tests/wgpu-gpu/queue_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
//! Tests for buffer copy validation.
use wgpu::PollType;
use wgpu_test::{fail, gpu_test, GpuTestConfiguration};

#[gpu_test]
static QUEUE_WRITE_TEXTURE_THEN_DESTROY: GpuTestConfiguration = GpuTestConfiguration::new()
.run_sync(|ctx| {
let texture = ctx.device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 64,
height: 32,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba32Float,
usage: wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});

let data = vec![255; 1024];

ctx.queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d { x: 0, y: 0, z: 0 },
aspect: wgpu::TextureAspect::All,
},
&data,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(1024),
//bytes_per_image: 4294967295,
rows_per_image: Some(32),
},
wgpu::Extent3d {
width: 64,
height: 1,
depth_or_array_layers: 1,
},
);

// Unlike textures used in a command buffer, which must not be destroyed prior to calling
// submit, it is permissible to destroy a texture used in an immediate queue operation
// before calling submit.
texture.destroy();

ctx.queue.submit([]);
ctx.device.poll(PollType::wait()).unwrap();
});

#[gpu_test]
static QUEUE_WRITE_TEXTURE_OVERFLOW: GpuTestConfiguration =
GpuTestConfiguration::new().run_sync(|ctx| {
Expand Down
12 changes: 12 additions & 0 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ pub(crate) struct PendingWrites {
pub is_recording: bool,

temp_resources: Vec<TempResource>,
temp_textures: FastHashMap<TrackerIndex, DestroyedTexture>,
dst_buffers: FastHashMap<TrackerIndex, Arc<Buffer>>,
dst_textures: FastHashMap<TrackerIndex, Arc<Texture>>,
copied_blas_s: FastHashMap<TrackerIndex, Arc<Blas>>,
Expand All @@ -334,6 +335,7 @@ impl PendingWrites {
command_encoder,
is_recording: false,
temp_resources: Vec::new(),
temp_textures: FastHashMap::default(),
dst_buffers: FastHashMap::default(),
dst_textures: FastHashMap::default(),
copied_blas_s: FastHashMap::default(),
Expand Down Expand Up @@ -367,6 +369,10 @@ impl PendingWrites {
self.temp_resources.push(resource);
}

pub fn consume_texture(&mut self, index: TrackerIndex, resource: DestroyedTexture) {
self.temp_textures.insert(index, resource);
}

pub fn consume(&mut self, buffer: FlushedStagingBuffer) {
self.temp_resources
.push(TempResource::StagingBuffer(buffer));
Expand Down Expand Up @@ -1279,6 +1285,12 @@ impl Queue {
.unwrap()
};
}
// It's okay if the texture was destroyed if we still
// have the "pending deletion" handle to it.
Err(DestroyedResourceError(_))
if pending_writes
.temp_textures
.contains_key(&texture.tracker_index()) => {}
Err(e) => break 'error Err(e.into()),
}
}
Expand Down
11 changes: 7 additions & 4 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,25 +1203,28 @@ impl Texture {
mem::take(&mut *guard)
};

queue::TempResource::DestroyedTexture(DestroyedTexture {
DestroyedTexture {
raw: ManuallyDrop::new(raw),
views,
clear_mode: mem::replace(&mut *self.clear_mode.write(), TextureClearMode::None),
bind_groups,
device: Arc::clone(&self.device),
label: self.label().to_owned(),
})
}
};

if let Some(queue) = device.get_queue() {
let mut pending_writes = queue.pending_writes.lock();
if pending_writes.contains_texture(self) {
pending_writes.consume_temp(temp);
pending_writes.consume_texture(self.tracker_index(), temp);
} else {
let mut life_lock = queue.lock_life();
let last_submit_index = life_lock.get_texture_latest_submission_index(self);
if let Some(last_submit_index) = last_submit_index {
life_lock.schedule_resource_destruction(temp, last_submit_index);
life_lock.schedule_resource_destruction(
queue::TempResource::DestroyedTexture(temp),
last_submit_index,
);
}
}
}
Expand Down
Loading