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

Transition resources #6678

Merged
merged 31 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cb8acef
WIP
JMS55 Dec 6, 2024
fd4b993
Fix typo
JMS55 Dec 6, 2024
8000e37
WIP: Implement structure of command_encoder_transition_resources
JMS55 Dec 6, 2024
a441f81
WIP
JMS55 Dec 7, 2024
e19769c
More work
JMS55 Dec 7, 2024
efbb336
Clippy
JMS55 Dec 7, 2024
1897bca
Fix web build
JMS55 Dec 7, 2024
328028b
Use new types for API, more docs
JMS55 Dec 7, 2024
8fc87bb
Add very basic test
JMS55 Dec 7, 2024
8d711c9
Try to fix test cfg
JMS55 Dec 7, 2024
fea7b90
Merge commit 'a8a91737b2d2f378976e292074c75817593a0224' into transiti…
JMS55 Jan 7, 2025
dacb259
Fix merge
JMS55 Jan 8, 2025
27497ce
Merge commit '05e62f96f972981da6a5919609cc08b1c9ac8f54' into transiti…
JMS55 Jan 12, 2025
03084df
Missed commit
JMS55 Jan 12, 2025
1590a1d
Use wgt types instead of hal types
JMS55 Jan 13, 2025
fb3cb54
Implement `Clone` for `ShaderModule` (#6939)
a1phyr Jan 17, 2025
1c13ef9
Move to dispatch trait, move more things to wgt
JMS55 Jan 18, 2025
4e46c96
Move existing code to use new wgt types
JMS55 Jan 18, 2025
0e98d93
Fixes
JMS55 Jan 18, 2025
3dd0525
Merge commit 'fb3cb547fdd989f9c85b94d305fa5b6925c7529b' into transiti…
JMS55 Jan 18, 2025
08807c3
Format import
JMS55 Jan 18, 2025
8e3b178
Format another file
JMS55 Jan 18, 2025
b81c1c9
Fixes
JMS55 Jan 18, 2025
2be3395
Merge branch 'trunk' into transition_resources
cwfitzgerald Jan 23, 2025
cefa9ba
Make module private
JMS55 Jan 23, 2025
66aacba
Merge commit 'd8e7ab1ad13f2bf2f9702401d1bc625e26b1c2e6' into transiti…
JMS55 Jan 24, 2025
4e49e5d
Fix imports
JMS55 Jan 24, 2025
2f2e9dd
Fix test imports
JMS55 Jan 24, 2025
11a6d37
Rexport types
JMS55 Jan 24, 2025
a90542e
Fix imports
JMS55 Jan 24, 2025
4483082
Fix import
JMS55 Jan 24, 2025
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]
- Image atomic support in shaders. By @atlv24 in [#6706](https://github.com/gfx-rs/wgpu/pull/6706)
- 64 bit image atomic support in shaders. By @atlv24 in [#5537](https://github.com/gfx-rs/wgpu/pull/5537)
- Add `no_std` support to `wgpu-types`. By @bushrat011899 in [#6892](https://github.com/gfx-rs/wgpu/pull/6892).
- Added `CommandEncoder::transition_resources()` for native API interop, and allowing users to slightly optimize barriers. By @JMS55 in [6678](https://github.com/gfx-rs/wgpu/pull/6678).

##### Vulkan

Expand Down
1 change: 1 addition & 0 deletions tests/tests/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod texture_blit;
mod texture_bounds;
mod texture_view_creation;
mod transfer;
mod transition_resources;
mod vertex_formats;
mod vertex_indices;
mod write_texture;
Expand Down
35 changes: 35 additions & 0 deletions tests/tests/transition_resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use wgpu_test::{gpu_test, GpuTestConfiguration};

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

let mut encoder = ctx
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

encoder.transition_resources(
std::iter::empty(),
[wgpu::TextureTransition {
texture: &texture,
selector: None,
state: wgpu::TextureUses::COLOR_TARGET,
}]
.into_iter(),
);

ctx.queue.submit([encoder.finish()]);
});
6 changes: 3 additions & 3 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ fn set_index_buffer(
state
.trackers
.buffers
.merge_single(&buffer, hal::BufferUses::INDEX)?;
.merge_single(&buffer, wgt::BufferUses::INDEX)?;

buffer.same_device(&state.device)?;
buffer.check_usage(wgt::BufferUsages::INDEX)?;
Expand Down Expand Up @@ -725,7 +725,7 @@ fn set_vertex_buffer(
state
.trackers
.buffers
.merge_single(&buffer, hal::BufferUses::VERTEX)?;
.merge_single(&buffer, wgt::BufferUses::VERTEX)?;

buffer.same_device(&state.device)?;
buffer.check_usage(wgt::BufferUsages::VERTEX)?;
Expand Down Expand Up @@ -864,7 +864,7 @@ fn multi_draw_indirect(
state
.trackers
.buffers
.merge_single(&buffer, hal::BufferUses::INDIRECT)?;
.merge_single(&buffer, wgt::BufferUses::INDIRECT)?;

buffer.same_device(&state.device)?;
buffer.check_usage(wgt::BufferUsages::INDIRECT)?;
Expand Down
19 changes: 11 additions & 8 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ use crate::{
ParentDevice, ResourceErrorIdent, Texture, TextureClearMode,
},
snatch::SnatchGuard,
track::{TextureSelector, TextureTrackerSetSingle},
track::TextureTrackerSetSingle,
};

use thiserror::Error;
use wgt::{math::align_to, BufferAddress, BufferUsages, ImageSubresourceRange, TextureAspect};
use wgt::{
math::align_to, BufferAddress, BufferUsages, ImageSubresourceRange, TextureAspect,
TextureSelector,
};

/// Error encountered while attempting a clear.
#[derive(Clone, Debug, Error)]
Expand Down Expand Up @@ -107,7 +110,7 @@ impl Global {
let dst_pending = cmd_buf_data
.trackers
.buffers
.set_single(&dst_buffer, hal::BufferUses::COPY_DST);
.set_single(&dst_buffer, wgt::BufferUses::COPY_DST);

let snatch_guard = dst_buffer.device.snatchable_lock.read();
let dst_raw = dst_buffer.try_raw(&snatch_guard)?;
Expand Down Expand Up @@ -269,12 +272,12 @@ pub(crate) fn clear_texture<T: TextureTrackerSetSingle>(

// Issue the right barrier.
let clear_usage = match dst_texture.clear_mode {
TextureClearMode::BufferCopy => hal::TextureUses::COPY_DST,
TextureClearMode::BufferCopy => wgt::TextureUses::COPY_DST,
TextureClearMode::RenderPass {
is_color: false, ..
} => hal::TextureUses::DEPTH_STENCIL_WRITE,
} => wgt::TextureUses::DEPTH_STENCIL_WRITE,
TextureClearMode::Surface { .. } | TextureClearMode::RenderPass { is_color: true, .. } => {
hal::TextureUses::COLOR_TARGET
wgt::TextureUses::COLOR_TARGET
}
TextureClearMode::None => {
return Err(ClearError::NoValidTextureClearMode(
Expand Down Expand Up @@ -455,7 +458,7 @@ fn clear_texture_via_render_passes(
mip_level,
depth_or_layer,
),
usage: hal::TextureUses::COLOR_TARGET,
usage: wgt::TextureUses::COLOR_TARGET,
},
resolve_target: None,
ops: hal::AttachmentOps::STORE,
Expand All @@ -473,7 +476,7 @@ fn clear_texture_via_render_passes(
mip_level,
depth_or_layer,
),
usage: hal::TextureUses::DEPTH_STENCIL_WRITE,
usage: wgt::TextureUses::DEPTH_STENCIL_WRITE,
},
depth_ops: hal::AttachmentOps::STORE,
stencil_ops: hal::AttachmentOps::STORE,
Expand Down
12 changes: 6 additions & 6 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ fn dispatch_indirect(
let src_transition = state
.intermediate_trackers
.buffers
.set_single(&buffer, hal::BufferUses::STORAGE_READ_ONLY);
.set_single(&buffer, wgt::BufferUses::STORAGE_READ_ONLY);
let src_barrier =
src_transition.map(|transition| transition.into_hal(&buffer, &state.snatch_guard));
unsafe {
Expand All @@ -949,8 +949,8 @@ fn dispatch_indirect(
state.raw_encoder.transition_buffers(&[hal::BufferBarrier {
buffer: params.dst_buffer,
usage: hal::StateTransition {
from: hal::BufferUses::INDIRECT,
to: hal::BufferUses::STORAGE_READ_WRITE,
from: wgt::BufferUses::INDIRECT,
to: wgt::BufferUses::STORAGE_READ_WRITE,
},
}]);
}
Expand Down Expand Up @@ -996,8 +996,8 @@ fn dispatch_indirect(
state.raw_encoder.transition_buffers(&[hal::BufferBarrier {
buffer: params.dst_buffer,
usage: hal::StateTransition {
from: hal::BufferUses::STORAGE_READ_WRITE,
to: hal::BufferUses::INDIRECT,
from: wgt::BufferUses::STORAGE_READ_WRITE,
to: wgt::BufferUses::INDIRECT,
},
}]);
}
Expand All @@ -1012,7 +1012,7 @@ fn dispatch_indirect(
state
.scope
.buffers
.merge_single(&buffer, hal::BufferUses::INDIRECT)?;
.merge_single(&buffer, wgt::BufferUses::INDIRECT)?;

use crate::resource::Trackable;
state.flush_states(Some(buffer.tracker_index()))?;
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/command/memory_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl BakedCommands {
// must already know about it.
let transition = device_tracker
.buffers
.set_single(&buffer, hal::BufferUses::COPY_DST);
.set_single(&buffer, wgt::BufferUses::COPY_DST);

let raw_buf = buffer.try_raw(snatch_guard)?;

Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod render;
mod render_command;
mod timestamp_writes;
mod transfer;
mod transition_resources;

use std::mem::{self, ManuallyDrop};
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl Global {
let dst_pending = cmd_buf_data
.trackers
.buffers
.set_single(&dst_buffer, hal::BufferUses::COPY_DST);
.set_single(&dst_buffer, wgt::BufferUses::COPY_DST);

let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard));

Expand Down
3 changes: 1 addition & 2 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ use crate::{
FastHashSet,
};

use wgt::{math::align_to, BufferUsages, Features};
use wgt::{math::align_to, BufferUsages, BufferUses, Features};

use super::CommandBufferMutable;
use hal::BufferUses;
use std::{
cmp::max,
num::NonZeroU64,
Expand Down
32 changes: 16 additions & 16 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ use crate::{
DestroyedResourceError, Labeled, MissingBufferUsageError, MissingTextureUsageError,
ParentDevice, QuerySet, Texture, TextureView, TextureViewNotRenderableReason,
},
track::{ResourceUsageCompatibilityError, TextureSelector, Tracker, UsageScope},
track::{ResourceUsageCompatibilityError, Tracker, UsageScope},
Label,
};

use arrayvec::ArrayVec;
use thiserror::Error;
use wgt::{
BufferAddress, BufferSize, BufferUsages, Color, DynamicOffset, IndexFormat, ShaderStages,
TextureUsages, TextureViewDimension, VertexStepMode,
TextureSelector, TextureUsages, TextureViewDimension, VertexStepMode,
};

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -779,11 +779,11 @@ where
struct RenderAttachment {
texture: Arc<Texture>,
selector: TextureSelector,
usage: hal::TextureUses,
usage: wgt::TextureUses,
}

impl TextureView {
fn to_render_attachment(&self, usage: hal::TextureUses) -> RenderAttachment {
fn to_render_attachment(&self, usage: wgt::TextureUses) -> RenderAttachment {
RenderAttachment {
texture: self.parent.clone(),
selector: self.selector.clone(),
Expand Down Expand Up @@ -1049,9 +1049,9 @@ impl<'d> RenderPassInfo<'d> {
.flags
.contains(wgt::DownlevelFlags::READ_ONLY_DEPTH_STENCIL)
{
hal::TextureUses::DEPTH_STENCIL_READ | hal::TextureUses::RESOURCE
wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::RESOURCE
} else {
hal::TextureUses::DEPTH_STENCIL_WRITE
wgt::TextureUses::DEPTH_STENCIL_WRITE
};
render_attachments.push(view.to_render_attachment(usage));

Expand Down Expand Up @@ -1104,7 +1104,7 @@ impl<'d> RenderPassInfo<'d> {
&mut pending_discard_init_fixups,
);
render_attachments
.push(color_view.to_render_attachment(hal::TextureUses::COLOR_TARGET));
.push(color_view.to_render_attachment(wgt::TextureUses::COLOR_TARGET));

let mut hal_resolve_target = None;
if let Some(resolve_view) = &at.resolve_target {
Expand Down Expand Up @@ -1160,18 +1160,18 @@ impl<'d> RenderPassInfo<'d> {
TextureInitRange::from(resolve_view.selector.clone()),
);
render_attachments
.push(resolve_view.to_render_attachment(hal::TextureUses::COLOR_TARGET));
.push(resolve_view.to_render_attachment(wgt::TextureUses::COLOR_TARGET));

hal_resolve_target = Some(hal::Attachment {
view: resolve_view.try_raw(snatch_guard)?,
usage: hal::TextureUses::COLOR_TARGET,
usage: wgt::TextureUses::COLOR_TARGET,
});
}

color_attachments_hal.push(Some(hal::ColorAttachment {
target: hal::Attachment {
view: color_view.try_raw(snatch_guard)?,
usage: hal::TextureUses::COLOR_TARGET,
usage: wgt::TextureUses::COLOR_TARGET,
},
resolve_target: hal_resolve_target,
ops: at.hal_ops(),
Expand Down Expand Up @@ -1333,7 +1333,7 @@ impl<'d> RenderPassInfo<'d> {
depth_stencil_attachment: Some(hal::DepthStencilAttachment {
target: hal::Attachment {
view: view.try_raw(snatch_guard)?,
usage: hal::TextureUses::DEPTH_STENCIL_WRITE,
usage: wgt::TextureUses::DEPTH_STENCIL_WRITE,
},
depth_ops,
stencil_ops,
Expand Down Expand Up @@ -2167,7 +2167,7 @@ fn set_index_buffer(
.info
.usage_scope
.buffers
.merge_single(&buffer, hal::BufferUses::INDEX)?;
.merge_single(&buffer, wgt::BufferUses::INDEX)?;

buffer.same_device_as(cmd_buf.as_ref())?;

Expand Down Expand Up @@ -2216,7 +2216,7 @@ fn set_vertex_buffer(
.info
.usage_scope
.buffers
.merge_single(&buffer, hal::BufferUses::VERTEX)?;
.merge_single(&buffer, wgt::BufferUses::VERTEX)?;

buffer.same_device_as(cmd_buf.as_ref())?;

Expand Down Expand Up @@ -2496,7 +2496,7 @@ fn multi_draw_indirect(
.info
.usage_scope
.buffers
.merge_single(&indirect_buffer, hal::BufferUses::INDIRECT)?;
.merge_single(&indirect_buffer, wgt::BufferUses::INDIRECT)?;

indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
let indirect_raw = indirect_buffer.try_raw(state.snatch_guard)?;
Expand Down Expand Up @@ -2573,7 +2573,7 @@ fn multi_draw_indirect_count(
.info
.usage_scope
.buffers
.merge_single(&indirect_buffer, hal::BufferUses::INDIRECT)?;
.merge_single(&indirect_buffer, wgt::BufferUses::INDIRECT)?;

indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
let indirect_raw = indirect_buffer.try_raw(state.snatch_guard)?;
Expand All @@ -2582,7 +2582,7 @@ fn multi_draw_indirect_count(
.info
.usage_scope
.buffers
.merge_single(&count_buffer, hal::BufferUses::INDIRECT)?;
.merge_single(&count_buffer, wgt::BufferUses::INDIRECT)?;

count_buffer.check_usage(BufferUsages::INDIRECT)?;
let count_raw = count_buffer.try_raw(state.snatch_guard)?;
Expand Down
Loading
Loading