diff --git a/crates/metal2vulkan-runtime/src/framebuffer_fetch.rs b/crates/metal2vulkan-runtime/src/framebuffer_fetch.rs index a86f7b165..3f62b57b5 100644 --- a/crates/metal2vulkan-runtime/src/framebuffer_fetch.rs +++ b/crates/metal2vulkan-runtime/src/framebuffer_fetch.rs @@ -34,10 +34,10 @@ //! Races: "A vkCmdPipelineBarrier inside a subpass can synchronize prior attachment writes in the //! subpass with subsequent input attachment reads"), which every pass has //! (`render_target::DRAWS_SELF_DEPENDENCY`). So a draw that reads input attachments after an -//! earlier draw of its render pass instance wrote a color, and a draw that writes a color after an -//! earlier one read input attachments, records after that barrier (`render.rs`'s -//! `InterlockedStorage`, `WorkCounts::fetch_draw_barriers`): every draw reads what the draws before -//! it stored, Metal's answer. Nothing orders the overlapping fragments of one draw there, which the +//! earlier draw of its render pass instance, or its load operations, wrote a color, and a draw +//! that writes a color after an earlier one read input attachments, records after that barrier +//! (`render.rs`'s `InterlockedStorage`, `WorkCounts::fetch_draw_barriers`): every draw reads what +//! the draws before it stored, Metal's answer. Nothing orders the overlapping fragments of one draw there, which the //! fetching fragment stage records (`PrecisionNote::UnorderedFetchWithinDraw`, announced as the //! stage's other notes are, where the pipeline is made): such a fragment may read the texel from //! before an earlier fragment of its draw wrote it. diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 0e787cba9..4fa11bc8b 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -1037,8 +1037,9 @@ impl DrawStates { /// (`framebuffer_fetch::inputs_between_draws`: neither rasterization-order color attachment access /// nor the pixel interlock), the colors a pass's draws fetch take two more kinds, which the /// barrier of the subpass's whole dependency on itself orders (`render_target::between_draws`): -/// a draw that reads input attachments after an earlier draw of its instance wrote a color, and -/// a draw that writes a color after an earlier one read input attachments, records after it. No +/// a draw that reads input attachments after an earlier draw of its instance, or its load +/// operations (`BEGUN`), wrote a color, and a draw that writes a color after an +/// earlier one read input attachments, records after it. No /// interlock orders anything there; the barrier orders the draws, as Vulkan's subpass /// self-dependency does ("A vkCmdPipelineBarrier inside a subpass can synchronize prior attachment /// writes in the subpass with subsequent input attachment reads", the Render Pass chapter's @@ -1072,6 +1073,18 @@ impl InterlockedStorage { writes_after_fetch: false, }; + /// What a render pass instance's draws meet before any of them: its colors' load operations' + /// writes, which a draw that reads input attachments ordered between draws alone reaches + /// (`fetch_after_writes`). Vulkan orders the load operations before every access of the + /// instance through the attachment, but RADV on the RX 550 (Polaris) was measured to read a + /// load-cleared `RGBA32Uint` attachment's earlier contents at the instance's first fetch + /// without the barrier, and the barrier is the one between draws, inside the subpass's + /// dependency on itself; no other kind is set, so on every other device it orders nothing. + const BEGUN: Self = Self { + fetch_after_writes: true, + ..Self::NONE + }; + /// Every kind: what an execution of render commands, or the encoder's draws before it, may /// have written. const ALL: Self = Self { @@ -2291,7 +2304,7 @@ impl RenderLowering { shadow: false, shader_tests: false, imageblock: false, - interlocked_writes: InterlockedStorage::NONE, + interlocked_writes: InterlockedStorage::BEGUN, indirect_reads: IndirectReads::new(), geometry_writes: GeometryWrites::new(), tile_memory: false, @@ -6997,7 +7010,7 @@ impl RenderLowering { shadow, shader_tests, imageblock: false, - interlocked_writes: InterlockedStorage::NONE, + interlocked_writes: InterlockedStorage::BEGUN, indirect_reads: IndirectReads::new(), geometry_writes: GeometryWrites::new(), tile_memory: false, @@ -7112,7 +7125,7 @@ impl RenderLowering { shadow: false, shader_tests: false, imageblock: false, - interlocked_writes: InterlockedStorage::NONE, + interlocked_writes: InterlockedStorage::BEGUN, indirect_reads: IndirectReads::new(), geometry_writes: GeometryWrites::new(), tile_memory: false, diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 4ab3e6aff..d302e212e 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -7722,8 +7722,8 @@ pub struct WorkCounts { /// Barriers recorded before a draw on a device with neither rasterization-order color /// attachment access nor `fragmentShaderPixelInterlock`, whose framebuffer fetch reads input /// attachments ordered between draws alone: before a draw that reads them after an earlier - /// draw of its render pass instance wrote a color, or that writes a color after an earlier - /// one read them, each the draws' subpass's whole dependency on itself + /// draw of its render pass instance, or its load operations, wrote a color, or that writes a + /// color after an earlier one read them, each the draws' subpass's whole dependency on itself /// (`framebuffer_fetch.rs`). 0 on every other device. pub fetch_draw_barriers: u64, /// Draws that sample a color attachment's texture after a memory barrier of their render diff --git a/crates/metal2vulkan-validation/src/runtime_lane/framebuffer_fetch/between_draws.rs b/crates/metal2vulkan-validation/src/runtime_lane/framebuffer_fetch/between_draws.rs index e686224a7..7aa7885c3 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/framebuffer_fetch/between_draws.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/framebuffer_fetch/between_draws.rs @@ -6,25 +6,28 @@ //! //! The runtime reads each fetched color as the input attachment of its index and records a barrier //! of the draws' subpass's dependency on itself, by region, before a draw that reads input -//! attachments after an earlier draw of its render pass instance wrote a color, and before a draw -//! that writes a color after an earlier one read them (`WorkCounts::fetch_draw_barriers`). Each -//! pass draws the fullscreen triangle (`rt6_resolves`'s `rt6_resolve_full`) over a 4² target: +//! attachments after an earlier draw of its render pass instance, or its load operations, wrote a +//! color, and before a draw that writes a color after an earlier one read them +//! (`WorkCounts::fetch_draw_barriers`). Each pass draws the fullscreen triangle (`rt6_resolves`'s +//! `rt6_resolve_full`) over a 4² target: //! //! - **Draws in order**: an `R32Float` target cleared to 0.75, then `fetch_scalar` //! (`fragment_fetch`, half what it reads), `rt6_resolve_white` (1), `fetch_scalar` twice: 0.25, //! Metal's answer, which holds only where each draw reads what the draw before it stored (a read -//! of the clear in each fetch leaves 0.375; a fetch that missed the white write, 0.09375). Three -//! barriers: before the white write (after the first fetch's reads) and before each later fetch -//! (after the write before it). +//! of the clear in each fetch leaves 0.375; a fetch that missed the white write, 0.09375). Four +//! barriers: before the first fetch (after the clear), before the white write (after the first +//! fetch's reads) and before each later fetch (after the write before it). //! - **Integer read-modify-writes**: an `RGBA32Uint` target cleared to (1, 2, 3, 4), then //! `exf_fetch_count` (three times what it reads, plus `k + 1` in lane `k`) three times: -//! `c · 27 + (k + 1) · 13`, exactly. Two barriers. +//! `c · 27 + (k + 1) · 13`, exactly, which holds only where the first draw reads the clear (one +//! that reads 0 leaves `(k + 1) · 13`, as RADV on the RX 550 did without the first barrier). +//! Three barriers. //! - **One draw over itself**: `fetch_scalar` drawn as two instances of the fullscreen triangle in //! one draw over the `R32Float` target cleared to 0.75. Metal halves each texel twice (0.1875). //! Nothing orders the second instance's fragments after the first's here, so each texel holds //! 0.1875 or 0.375 (the second read before the first's write landed), printed with its count; -//! the pipeline's fragment stage records `PrecisionNote::UnorderedFetchWithinDraw`. No barrier: -//! one draw. +//! the pipeline's fragment stage records `PrecisionNote::UnorderedFetchWithinDraw`. One barrier, +//! after the clear: nothing comes between the draw's fragments. //! //! The runtime announces the note (`unordered-fetch-within-draw`) once, where the first fetching //! pipeline is made, as it announces a stage's other notes; no draw announces it again. @@ -115,7 +118,7 @@ fn fetches_between_draws_read_every_earlier_draw_and_note_the_draw_itself() { let announced = counts(&messages.take_notices()); assert_eq!(announced.get(¬e()), Some(&1), "{device}: {announced:?}"); - // Draws in order: 0.25, three barriers. + // Draws in order: 0.25, four barriers. let before = barriers(&lane); let target = lane.texture(pf, ONE, TextureUsage::RENDER_TARGET); let clear = ClearColor::new(0.75, 0.0, 0.0, 0.0); @@ -134,12 +137,12 @@ fn fetches_between_draws_read_every_earlier_draw_and_note_the_draw_itself() { ); assert_eq!( barriers(&lane) - before, - 3, + 4, "{device}: barriers between the draws" ); - eprintln!("FETCH-BETWEEN-DRAWS draws in order on {device}: 0.25 at every texel, 3 barriers"); + eprintln!("FETCH-BETWEEN-DRAWS draws in order on {device}: 0.25 at every texel, 4 barriers"); - // Integer read-modify-writes: exact, two barriers. + // Integer read-modify-writes: exact, three barriers. let integers = [PixelFormat::RGBA32Uint]; let step = pipeline(&lane, &vertices, (&count, "exf_fetch_count"), &integers, 1).unwrap(); let before = barriers(&lane); @@ -155,12 +158,13 @@ fn fetches_between_draws_read_every_earlier_draw_and_note_the_draw_itself() { ); assert_eq!( barriers(&lane) - before, - 2, + 3, "{device}: barriers between the draws" ); - eprintln!("FETCH-BETWEEN-DRAWS integer read-modify-writes on {device}: exact, 2 barriers"); + eprintln!("FETCH-BETWEEN-DRAWS integer read-modify-writes on {device}: exact, 3 barriers"); - // One draw over itself: each texel one of the two orders of its fragments, no barrier. + // One draw over itself: each texel one of the two orders of its fragments, the one barrier + // after the clear. let before = barriers(&lane); let target = lane.texture(pf, ONE, TextureUsage::RENDER_TARGET); let clear = ClearColor::new(0.75, 0.0, 0.0, 0.0); @@ -191,7 +195,7 @@ fn fetches_between_draws_read_every_earlier_draw_and_note_the_draw_itself() { } assert_eq!( barriers(&lane) - before, - 0, + 1, "{device}: a barrier inside one draw" ); eprintln!( diff --git a/docs/API.md b/docs/API.md index f51759966..a5f6cd58c 100644 --- a/docs/API.md +++ b/docs/API.md @@ -845,8 +845,8 @@ Where Metal's rules differ from Vulkan's, the pipeline gives Metal's answer: as an input attachment without rasterization-order access (`GraphicsState::rasterization_order_attachment_access` false), and the consumer records a barrier of the subpass's dependency on itself, by region, before each draw that reads input - attachments after an earlier draw of the subpass wrote a color, and before each draw that writes - a color after an earlier one read them: a fetch reads what every earlier draw stored, Metal's + attachments after an earlier draw of the subpass, or its load operations, wrote a color, and + before each draw that writes a color after an earlier one read them: a fetch reads what every earlier draw stored, Metal's answer, but the overlapping fragments of one draw are not ordered, which the fragment stage records (`PrecisionNote::UnorderedFetchWithinDraw`; the runtime does all of this itself). A raster order group on such a color input, or on a color output of an attachment the function @@ -3664,8 +3664,8 @@ bound. `fragmentShaderPixelInterlock` (Honeykrisp on Apple's M2 Pro; RADV on the RX 550). It reads the attachment as an input attachment, and the runtime records a barrier of the draws' subpass's dependency on itself, by region, before each draw that reads input attachments after an earlier - draw of its render pass instance wrote a color, and before each draw that writes a color after - an earlier one read them (`WorkCounts::fetch_draw_barriers`): a fetch reads what every earlier + draw of its render pass instance, or its load operations, wrote a color, and before each draw + that writes a color after an earlier one read them (`WorkCounts::fetch_draw_barriers`): a fetch reads what every earlier draw stored at its pixel, Metal's answer. Vulkan orders nothing between the overlapping fragments of one draw there, where Metal orders them in primitive order: a fragment that covers a pixel an earlier fragment of its own draw covered may read the texel from before that