rt: a draw reads an attachment of its own pass through a texture, through the attachment feedback loop
Metal lets a draw read, through a texture, a render target its own pass renders; the streams do so in about 16k (r3) and 13k (b3) passes, always a shader-read colour render target of Private storage, from the fragment function. The runtime refused every such draw (DrawKind::AttachmentRead). Where the device lists VK_EXT_attachment_feedback_loop_layout and VK_EXT_attachment_feedback_loop_dynamic_state, reports both features and its descriptor limits hold 2 x 65,536 sampled textures (floor::ATTACHMENT_READ_EXTENSIONS, ATTACHMENT_READS, feedback_room; Capabilities::attachment_reads), device creation enables them, and: - a shader-read, render-target colour image with no storage view and home SHADER_READ_ONLY_OPTIMAL takes ATTACHMENT_FEEDBACK_LOOP usage (where the format query allows it with that usage), and each texture of it a second element in set 1's sampled array at index + 65,536, written in ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT; - every render pipeline takes ATTACHMENT_FEEDBACK_LOOP_ENABLE_EXT as dynamic state; the encoder sets it at its first draw and where it changes, so a pass of no reading draw pays one call; - the pass's first draw that reads such an attachment ends the rendering, moves the whole image to the feedback layout in one barrier (the rendered level and layer from the attachment layout, the rest from home) and begins again; until the pass ends, draws push the feedback elements of that image's textures, and the pass's second barrier moves the whole image home; - a memory barrier after draws that only rendered, in a pass that moved an image, is owed to the next reading draw, which pays it by ending the rendering, one global barrier and beginning again. That orders every pixel, the draw's own and any other, as Metal's render-target barrier does. One form for that barrier: the probe pair 395a2a5225 (a BY_REGION barrier inside the rendering, for draws whose every read is at their own pixel) and 8ee6a1865a (end, barrier, resume at every owed barrier), on two 1920x1080 BGRA8 rows with a barrier before every reading draw (fb_small, 200 draws under 64x64 scissors; fb_full, 32 full-screen draws), 4 rounds alternated, GPU p50 resume against by-region: XTX -0.0% / +0.1% (m3-max-46321), 3090 +0.2% / -0.1% (46322), RX 550 -0.1% / +0.0% (46324), Iris +0.1% / -0.0% (46323). CPU per draw: XTX +0.1% / -0.1%, 3090 -16.6% / -0.4%, RX 550 -1.2% / +2.6%; Iris's spread within an arm is about 40% and is re-timed alone. So VK_KHR_dynamic_rendering_local_read is not part of the condition, and the by-region branch is not built. A device without them refuses such a draw as before, and is otherwise the same (the UHD 630 lists none of the three). Tests: every_device_reads_its_attachment_through_a_texture_after_a_memory_barrier (own pixel and another pixel after a barrier, 5 barriers and 1 pass, or the refusal) and every_device_samples_a_lower_level_while_its_pass_renders_the_next; tests::floor holds the README's new extension rows and "Attachment-read feature" table. At the wip form of this change (8bc8d14b3c): check m3-max-46312 green (clippy x3, rustdoc, rt tests, lavapipe tests); legs 46329 (XTX, 2080), 46331 (3090, 780M), 46332 (RX 550, 780M, lavapipe), 46333 (Iris) printed the read path, 46334 (UHD) passed 37/37. Known gaps, in the README: a read through an argument buffer's texture of a moved image is not seen; the image moves to the feedback layout and back in every pass that reads it so.
This commit is contained in:
1 parent
3d1216b069
commit
1524689478
14 files changed
+796
-80
No files matched your search
@@ -255,6 +255,11 @@ integer texture (bindings 2, 3 and 4). Every binding is partially bound, updated
|
||||
updated while unused elements are pending, so an object's element is written once, as it is
|
||||
made, with no lock, and never rewritten for layouts.
|
||||
|
||||
- Where the device reads attachments (Floor, Attachment reads), binding 0 holds 131,072
|
||||
elements: element `i + 65,536` is texture `i`'s in the attachment feedback layout, written with
|
||||
element `i` for a texture whose image a pass may move there (a shader-read colour render target
|
||||
with no storage view), and a null descriptor otherwise. No grant reaches it; a draw pushes it
|
||||
in place of the texture's own index while the texture's image is in that layout.
|
||||
- Every element no object owns is defined. A texture element is a null descriptor
|
||||
(`nullDescriptor`): it reads zeros, sizes 0 and drops writes, as Metal's nil texture does. A
|
||||
sampler element is its array's default sampler, nearest and clamped to the edge; the
|
||||
@@ -1293,12 +1298,28 @@ or does not check is rebuilt from the generations' names, with young counted ful
|
||||
covers, so the scissor is Metal's rectangle cut to them; a rectangle past the render area,
|
||||
which Metal's validation stops, is cut to it, as plain Metal cuts it. A stencil reference
|
||||
saturates at 255, as Metal stores it.
|
||||
- A draw may read an attachment of its own pass through a texture, as Metal reads a render
|
||||
target, where the device reads attachments (Floor, Attachment reads): through the attachment
|
||||
feedback loop. The texture reaches the attachment's level and layer, the fragment function
|
||||
reads it and nothing writes it, and its image is a shader-read colour render target with no
|
||||
storage view, which one attachment of the pass renders. At the pass's first such draw the
|
||||
rendering ends, one barrier moves the whole image to the feedback layout (the attachment's
|
||||
level and layer from the attachment layout, the rest from home), and the rendering begins
|
||||
again with the attachment in that layout; the pass's second barrier moves the whole image
|
||||
home. Until then every draw of the pass reads the image's textures through their feedback
|
||||
elements (Set 1), a texture that reaches no rendered level among them. Every render pipeline
|
||||
takes the feedback loop's enable as dynamic state, which the encoder sets at its first draw and
|
||||
where it changes: colour for a draw that reads an attachment, none otherwise, so a pass of no
|
||||
such draw sets it once.
|
||||
- `memory_barrier` after a draw of the encoder that wrote memory ends the rendering, records one
|
||||
barrier and begins the rendering again, loading what it stored; after none it records nothing,
|
||||
as the pass's first barrier ordered everything before it. A draw that needs a copy first, of a
|
||||
texture over a buffer in an image of its own or over a Managed buffer's device copy, records it
|
||||
the same way, between the rendering's end and its new beginning. Fences record nothing, as in a
|
||||
compute encoder.
|
||||
barrier and begins the rendering again, loading what it stored. After draws that only rendered,
|
||||
in a pass that moved an image to the feedback layout, the barrier is owed to the next draw that
|
||||
reads an attachment: the rendering ends, one global barrier is recorded and it begins again,
|
||||
which orders every pixel the draw reads, its own and any other. Elsewhere it records nothing: the pass's first barrier ordered everything before
|
||||
it, and a move orders everything before it. A draw that needs a copy first, of a texture over a
|
||||
buffer in an image of its own or over a Managed buffer's device copy, records it the same way,
|
||||
between the rendering's end and its new beginning. Fences record nothing, as in a compute
|
||||
encoder.
|
||||
- Refused, as Metal's validation stops them: a draw with no pipeline state
|
||||
(`Refusal::NoPipeline`), a vertex buffer the vertex descriptor reads that is not bound
|
||||
(`Refusal::NoBufferAtIndex`) or bound at an offset past its length
|
||||
@@ -1313,8 +1334,15 @@ or does not check is rebuilt from the generations' names, with young counted ful
|
||||
tests or writes depth (no pass has a depth attachment yet), a viewport Vulkan's is not (a
|
||||
negative width, a depth range past `[0, 1]`, bounds past the device's viewport limits), a
|
||||
pipeline state whose formats are not the pass's attachments', and a draw that binds a texture
|
||||
whose image the pass renders into. The replay streams draw lines and triangles under
|
||||
depth-stencil states that test no depth.
|
||||
whose image the pass renders, where the feedback loop does not read it: on a device without the
|
||||
attachment reads, from the vertex function at the rendered level and layer, written, of a
|
||||
depth or stencil attachment, of an image with a storage view or over a buffer in place, or of
|
||||
an image two attachments of the pass render. The replay streams draw lines and triangles under
|
||||
depth-stencil states that test no depth, and every attachment they read through a texture is a
|
||||
shader-read colour render target of `Private` storage, read from the fragment function.
|
||||
- A draw that reaches an attachment of its own pass through an argument buffer's texture is not
|
||||
seen: it reads through the texture's own element, in its home layout, while the pass may have
|
||||
moved the image to the feedback layout.
|
||||
- A vertex fetch outside its buffer returns what the device gives: the floor enables no buffer
|
||||
robustness, so there is no bounds guarantee beyond the device's.
|
||||
- Not built, and so not methods: indirect draws, several viewports or scissor rectangles, the
|
||||
@@ -1330,7 +1358,9 @@ or does not check is rebuilt from the generations' names, with young counted ful
|
||||
of a 2D texture of one sample, with `Load`, `Clear` and `DontCare` loads and `Store` and
|
||||
`DontCare` stores.
|
||||
- An attachment whose home layout is shader-read-only (a texture of shader-read and render-target
|
||||
usage) is moved to the attachment layout and back by every pass that renders into it.
|
||||
usage) is moved to the attachment layout and back by every pass that renders into it, and to
|
||||
the feedback layout and back by a pass that reads it through a texture, also where the next
|
||||
pass over it reads it so again.
|
||||
|
||||
## One path for every device
|
||||
|
||||
@@ -1372,6 +1402,8 @@ Presenting runs on the runtime's own device; nothing is exported or imported.
|
||||
| `VK_KHR_shader_subgroup_uniform_control_flow` | device, where listed | SIMD groups' reconvergence where maximal reconvergence is not offered |
|
||||
| `VK_KHR_portability_enumeration` | instance, where offered | listing portability devices, each checked against the floor as any other |
|
||||
| `VK_KHR_portability_subset` | device, where listed | Vulkan requires it enabled where a device lists it; the device is asked for the portability features below |
|
||||
| `VK_EXT_attachment_feedback_loop_layout` | device, with the attachment reads | a draw's read of an attachment of its own pass through a texture, in the feedback layout (below) |
|
||||
| `VK_EXT_attachment_feedback_loop_dynamic_state` | device, with the attachment reads | the feedback loop's enable as dynamic state of every render pipeline |
|
||||
|
||||
### Features
|
||||
|
||||
@@ -1514,6 +1546,23 @@ The other four are one feature each:
|
||||
| `shaderMaximalReconvergence` | `VkPhysicalDeviceShaderMaximalReconvergenceFeaturesKHR` | SIMD groups that reconverge as Metal's do |
|
||||
| `shaderSubgroupUniformControlFlow` | `VkPhysicalDeviceShaderSubgroupUniformControlFlowFeaturesKHR` | SIMD groups' reconvergence where maximal reconvergence is not offered |
|
||||
|
||||
### Attachment reads
|
||||
|
||||
A draw that reads, through a texture, an attachment its own pass renders (Metal's read of a
|
||||
render target) is built where the device lists the two extensions marked "with the attachment
|
||||
reads" above, reports each feature below, and its descriptor limits hold the sampled-texture
|
||||
array's feedback elements beside the floor's arrays: 131,072 sampled textures where the limits
|
||||
above name 65,536 (`maxPerStageDescriptorUpdateAfterBindSampledImages`,
|
||||
`maxDescriptorSetUpdateAfterBindSampledImages`, `maxPerStageUpdateAfterBindResources` and
|
||||
`maxUpdateAfterBindDescriptorsInAllPools`). Device creation then enables all of them, and none
|
||||
otherwise; a device without them refuses such a draw (`Gap::Draw` of `DrawKind::AttachmentRead`),
|
||||
and is otherwise the same. `floor::Capabilities::attachment_reads` holds the answer.
|
||||
|
||||
| Attachment-read feature | Structure | Used for |
|
||||
|---|---|---|
|
||||
| `attachmentFeedbackLoopLayout` | `VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT` | the feedback layout and usage |
|
||||
| `attachmentFeedbackLoopDynamicState` | `VkPhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesEXT` | the enable as dynamic state |
|
||||
|
||||
The test `tests::floor` holds these tables equal to the code, checks that each missing item is
|
||||
named, and, under `--cfg m2v_gpu` or `--cfg m2v_cpu_icd`, checks every device the machine's
|
||||
Vulkan loader lists.
|
||||
@@ -18,6 +18,12 @@
|
||||
//! comparison (`MTLCompareFunctionNever`) does. Vulkan has no null sampler. The device writes
|
||||
//! every element once at creation ([`Arrays::new`]).
|
||||
//!
|
||||
//! Where the device reads attachments through textures (`floor::Capabilities::attachment_reads`),
|
||||
//! the sampled-texture array is twice as long: element `i + 65,536` is texture `i`'s in the
|
||||
//! attachment feedback layout, written with element `i` for a texture whose image a pass may move
|
||||
//! there (`crate::render_encoder`), and a null descriptor otherwise. No index of a grant reaches
|
||||
//! it; a draw pushes it in place of the texture's own while its image is in that layout.
|
||||
//!
|
||||
//! The consumer names each range of indices it takes ([`Device::grant_indices`]), so resource IDs
|
||||
//! are chosen on the guest's side and reproduce in replay. Three classes, disjoint: storage-capable
|
||||
//! textures take 1 to 4,095, one index valid in both texture arrays; other textures 4,096 to
|
||||
@@ -283,16 +289,21 @@ pub(crate) struct Arrays {
|
||||
}
|
||||
|
||||
impl Arrays {
|
||||
/// Makes set 1 and writes every element its default.
|
||||
/// Makes set 1 and writes every element its default; with `feedback`, the sampled-texture
|
||||
/// array holds the feedback elements too.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `device` is live and was made with the floor's features, `nullDescriptor` and the
|
||||
/// update-after-bind features among them.
|
||||
pub(crate) unsafe fn new(device: &ash::Device, counters: &Counters) -> Result<Arrays, Error> {
|
||||
pub(crate) unsafe fn new(
|
||||
device: &ash::Device,
|
||||
counters: &Counters,
|
||||
feedback: bool,
|
||||
) -> Result<Arrays, Error> {
|
||||
let mut made = Made::default();
|
||||
// SAFETY: the caller's contract; what was made is destroyed where a later step fails.
|
||||
let arrays = unsafe { made.make(device, counters) };
|
||||
let arrays = unsafe { made.make(device, counters, feedback) };
|
||||
if arrays.is_err() {
|
||||
// SAFETY: nothing else holds what was made.
|
||||
unsafe { made.destroy(device, counters) };
|
||||
@@ -312,12 +323,15 @@ impl Arrays {
|
||||
|
||||
/// Writes a texture's elements at `index` of `class`: the sampled-texture array's with
|
||||
/// `sampled` and its layout, and for a storage-capable texture the storage-texture array's
|
||||
/// with `storage`; a null descriptor where a view is `None`.
|
||||
/// with `storage`; a null descriptor where a view is `None`. With `feedback`, also the
|
||||
/// sampled-texture array's element at `index` plus [`SAMPLED_IMAGES`], with the sampled view
|
||||
/// in the attachment feedback layout.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller owns the elements (the index is occupied for it, or is being vacated by it), and
|
||||
/// each view is live and of the device, of an image in that layout whenever work uses it.
|
||||
/// each view is live and of the device, of an image in that layout whenever work uses it;
|
||||
/// `feedback` only where the set was made with the feedback elements.
|
||||
pub(crate) unsafe fn write_texture(
|
||||
&self,
|
||||
device: &ash::Device,
|
||||
@@ -325,17 +339,32 @@ impl Arrays {
|
||||
index: u32,
|
||||
sampled: Option<(vk::ImageView, vk::ImageLayout)>,
|
||||
storage: Option<vk::ImageView>,
|
||||
feedback: bool,
|
||||
) {
|
||||
let (view, layout) = sampled.unwrap_or((
|
||||
vk::ImageView::null(),
|
||||
vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
|
||||
));
|
||||
let looped = [vk::DescriptorImageInfo::default()
|
||||
.image_view(view)
|
||||
.image_layout(vk::ImageLayout::ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT)];
|
||||
let sampled = [vk::DescriptorImageInfo::default()
|
||||
.image_view(view)
|
||||
.image_layout(layout)];
|
||||
let storage = [vk::DescriptorImageInfo::default()
|
||||
.image_view(storage.unwrap_or_default())
|
||||
.image_layout(vk::ImageLayout::GENERAL)];
|
||||
if feedback {
|
||||
let write = write(
|
||||
self.set,
|
||||
SAMPLED_BINDING,
|
||||
vk::DescriptorType::SAMPLED_IMAGE,
|
||||
&looped,
|
||||
)
|
||||
.dst_array_element(index + SAMPLED_IMAGES);
|
||||
// SAFETY: as below; the element is the texture's alone.
|
||||
unsafe { device.update_descriptor_sets(&[write], &[]) };
|
||||
}
|
||||
let writes = [
|
||||
write(
|
||||
self.set,
|
||||
@@ -434,7 +463,13 @@ impl Made {
|
||||
/// # Safety
|
||||
///
|
||||
/// As [`Arrays::new`].
|
||||
unsafe fn make(&mut self, device: &ash::Device, counters: &Counters) -> Result<Arrays, Error> {
|
||||
unsafe fn make(
|
||||
&mut self,
|
||||
device: &ash::Device,
|
||||
counters: &Counters,
|
||||
feedback: bool,
|
||||
) -> Result<Arrays, Error> {
|
||||
let sampled = sampled_images(feedback);
|
||||
let empty = vk::DescriptorSetLayoutCreateInfo::default();
|
||||
// SAFETY: the device is live.
|
||||
unsafe { supported(device, &empty, 0) }?;
|
||||
@@ -445,7 +480,9 @@ impl Made {
|
||||
// SAFETY: the device is live, with the update-after-bind features of each binding's type
|
||||
// (the floor's).
|
||||
let layout = unsafe {
|
||||
with_arrays_layout(BINDINGS.map(|(_, _, count)| count), |info| {
|
||||
let mut counts = BINDINGS.map(|(_, _, count)| count);
|
||||
counts[SAMPLED_BINDING as usize] = sampled;
|
||||
with_arrays_layout(counts, |info| {
|
||||
supported(device, info, 1)?;
|
||||
device
|
||||
.create_descriptor_set_layout(info, None)
|
||||
@@ -456,7 +493,7 @@ impl Made {
|
||||
let sizes = [
|
||||
vk::DescriptorPoolSize {
|
||||
ty: vk::DescriptorType::SAMPLED_IMAGE,
|
||||
descriptor_count: SAMPLED_IMAGES,
|
||||
descriptor_count: sampled,
|
||||
},
|
||||
vk::DescriptorPoolSize {
|
||||
ty: vk::DescriptorType::STORAGE_IMAGE,
|
||||
@@ -503,7 +540,7 @@ impl Made {
|
||||
let compare = default(true)?;
|
||||
self.compare = Some(compare);
|
||||
// SAFETY: the set is the device's and no command buffer uses it yet.
|
||||
unsafe { fill(device, set, plain, compare) };
|
||||
unsafe { fill(device, set, sampled, plain, compare) };
|
||||
Ok(Arrays {
|
||||
empty,
|
||||
layout,
|
||||
@@ -583,23 +620,34 @@ pub(crate) unsafe fn supported(
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes every element of `set` its default: a null descriptor in each texture array, `plain`
|
||||
/// in the plain and integer-texture sampler arrays and `compare` in the comparison array.
|
||||
/// The elements of the sampled-texture array: [`SAMPLED_IMAGES`], twice that with the feedback
|
||||
/// elements.
|
||||
pub(crate) fn sampled_images(feedback: bool) -> u32 {
|
||||
match feedback {
|
||||
true => 2 * SAMPLED_IMAGES,
|
||||
false => SAMPLED_IMAGES,
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes every element of `set` its default: a null descriptor in each texture array, the
|
||||
/// sampled-texture array's `sampled` elements, `plain` in the plain and integer-texture sampler
|
||||
/// arrays and `compare` in the comparison array.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `set` is set 1 of `device`, `nullDescriptor` is enabled, and no other thread writes an element
|
||||
/// of it meanwhile.
|
||||
/// `set` is set 1 of `device`, of `sampled` sampled-texture elements, `nullDescriptor` is enabled,
|
||||
/// and no other thread writes an element of it meanwhile.
|
||||
unsafe fn fill(
|
||||
device: &ash::Device,
|
||||
set: vk::DescriptorSet,
|
||||
sampled: u32,
|
||||
plain: vk::Sampler,
|
||||
compare: vk::Sampler,
|
||||
) {
|
||||
let sampled = vec![
|
||||
vk::DescriptorImageInfo::default()
|
||||
.image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL);
|
||||
SAMPLED_IMAGES as usize
|
||||
sampled as usize
|
||||
];
|
||||
let storage = vec![
|
||||
vk::DescriptorImageInfo::default().image_layout(vk::ImageLayout::GENERAL);
|
||||
|
||||
@@ -505,7 +505,9 @@ pub struct Metrics {
|
||||
/// an encoder that wrote or a memory barrier call, recorded before the next command, two
|
||||
/// around each blit copy, fill and synchronize copy, and two around each render pass, the
|
||||
/// first of each pair paying what is owed, with one inside a render pass at a memory barrier
|
||||
/// after a draw that wrote memory. A draw takes none.
|
||||
/// after a draw that wrote memory, one at a draw that moves an attachment's image to the
|
||||
/// feedback layout, and one at a draw that reads an attachment after a memory barrier owed to
|
||||
/// it. Any other draw takes none.
|
||||
pub barriers: u64,
|
||||
/// Bytes the runtime copied for a call: `replace_region` and `get_bytes` staging, and the
|
||||
/// copies between a texture over a buffer and its image of its own
|
||||
|
||||
@@ -147,6 +147,10 @@ pub(crate) struct Core {
|
||||
pub(crate) ticks: Ticks,
|
||||
/// `VK_EXT_calibrated_timestamps`, the floor's: the GPU's clock read with the host's.
|
||||
pub(crate) calibrated: ash::ext::calibrated_timestamps::Device,
|
||||
/// `VK_EXT_attachment_feedback_loop_dynamic_state`'s command, where device creation enabled
|
||||
/// the attachment reads (`floor::Capabilities::attachment_reads`): every render pipeline takes
|
||||
/// the feedback loop's enable as dynamic state, which each draw sets.
|
||||
pub(crate) feedback: Option<ash::ext::attachment_feedback_loop_dynamic_state::Device>,
|
||||
/// The longest block: the device's `maxMemoryAllocationSize` or `maxBufferSize`, whichever is
|
||||
/// less.
|
||||
max_block_len: u64,
|
||||
@@ -334,6 +338,8 @@ impl Device {
|
||||
mut subgroup_uniform_control_flow,
|
||||
mut robustness2,
|
||||
mut portability,
|
||||
mut feedback_loop,
|
||||
mut feedback_loop_dynamic,
|
||||
} = enabled_features;
|
||||
let mut features = vk::PhysicalDeviceFeatures2::default()
|
||||
.features(core)
|
||||
@@ -352,6 +358,11 @@ impl Device {
|
||||
if enabled(ash::khr::portability_subset::NAME) {
|
||||
features = features.push_next(&mut portability);
|
||||
}
|
||||
if capabilities.attachment_reads {
|
||||
features = features
|
||||
.push_next(&mut feedback_loop)
|
||||
.push_next(&mut feedback_loop_dynamic);
|
||||
}
|
||||
// SAFETY: as for the floor check.
|
||||
let listed = unsafe { vk_instance.enumerate_device_extension_properties(raw) }
|
||||
.map_err(Error::vulkan("vkEnumerateDeviceExtensionProperties"))?;
|
||||
@@ -423,6 +434,9 @@ impl Device {
|
||||
families[family as usize].timestamp_valid_bits,
|
||||
);
|
||||
let calibrated = ash::ext::calibrated_timestamps::Device::new(vk_instance, &device);
|
||||
let feedback = (capabilities.attachment_reads).then(|| {
|
||||
ash::ext::attachment_feedback_loop_dynamic_state::Device::new(vk_instance, &device)
|
||||
});
|
||||
let limits = &properties.properties.limits;
|
||||
// The sampler arrays' two defaults count against the device's samplers too.
|
||||
let sampler_cap = limits.max_sampler_allocation_count.saturating_sub(2);
|
||||
@@ -459,6 +473,7 @@ impl Device {
|
||||
counters,
|
||||
ticks,
|
||||
calibrated,
|
||||
feedback,
|
||||
max_block_len,
|
||||
max_allocations,
|
||||
granularity,
|
||||
@@ -944,7 +959,7 @@ unsafe fn objects(
|
||||
// SAFETY: the device is live.
|
||||
let made = unsafe { Done::new(device) }.and_then(|mut done| {
|
||||
// SAFETY: the device is live, with the floor's features.
|
||||
let arrays = match unsafe { Arrays::new(device, counters) } {
|
||||
let arrays = match unsafe { Arrays::new(device, counters, capabilities.attachment_reads) } {
|
||||
Ok(arrays) => arrays,
|
||||
Err(error) => {
|
||||
// SAFETY: the completion objects are unused.
|
||||
|
||||
@@ -917,7 +917,12 @@ pub enum DrawKind {
|
||||
/// A pipeline state whose colour or stencil formats are not its pass's attachments': Metal's
|
||||
/// validation stops most such draws, and plain Metal zeroes or drops the colours.
|
||||
AttachmentFormats,
|
||||
/// A draw that binds a texture whose image the pass renders into.
|
||||
/// A draw that binds a texture whose image the pass renders, where the attachment feedback
|
||||
/// loop does not read it: on a device without the attachment reads
|
||||
/// (`floor::Capabilities::attachment_reads`), from the vertex function at the rendered level
|
||||
/// and layer, written, of a depth or stencil attachment, of an image with a storage view or
|
||||
/// over a buffer in place, or of an image two attachments of the pass render. Metal reads a
|
||||
/// render target through a texture.
|
||||
AttachmentRead,
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,10 @@ impl DeviceExtension {
|
||||
///
|
||||
/// `T` is a structure the floor's features do not already chain: not
|
||||
/// `VkPhysicalDeviceVulkan11Features`, `12`, `13`,
|
||||
/// `VkPhysicalDeviceShaderMaximalReconvergenceFeaturesKHR` or
|
||||
/// `VkPhysicalDeviceShaderSubgroupUniformControlFlowFeaturesKHR`.
|
||||
/// `VkPhysicalDeviceShaderMaximalReconvergenceFeaturesKHR`,
|
||||
/// `VkPhysicalDeviceShaderSubgroupUniformControlFlowFeaturesKHR`,
|
||||
/// `VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT` or
|
||||
/// `VkPhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesEXT`.
|
||||
pub fn with_features<T>(name: &CStr, wanted: T, needs: fn(&T) -> bool) -> DeviceExtension
|
||||
where
|
||||
T: vk::ExtendsPhysicalDeviceFeatures2
|
||||
|
||||
@@ -47,6 +47,16 @@ pub const OPTIONAL_EXTENSIONS: &[&CStr] = &[
|
||||
ash::khr::portability_subset::NAME,
|
||||
];
|
||||
|
||||
/// The device extensions of a draw that reads, through a texture, an attachment its own pass
|
||||
/// renders (Metal's texture read of a render target): device creation enables both, with their
|
||||
/// features, where the device lists both, reports both features and its descriptor limits hold the
|
||||
/// sampled-texture array's feedback elements, and neither otherwise. The read is the feedback
|
||||
/// loop's (`VK_EXT_attachment_feedback_loop_layout` and its dynamic state).
|
||||
pub const ATTACHMENT_READ_EXTENSIONS: &[&CStr] = &[
|
||||
ash::ext::attachment_feedback_loop_layout::NAME,
|
||||
ash::ext::attachment_feedback_loop_dynamic_state::NAME,
|
||||
];
|
||||
|
||||
/// The translator's name for an extension device creation enables that its shaders may use, or
|
||||
/// that its description of the device must enable: the portability subset, where the device lists
|
||||
/// it.
|
||||
@@ -175,6 +185,14 @@ pub struct Features {
|
||||
/// enabled only where the device lists it, and then only the portability features the floor asks
|
||||
/// of such a device.
|
||||
pub portability: vk::PhysicalDevicePortabilitySubsetFeaturesKHR<'static>,
|
||||
/// `VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT`, of
|
||||
/// `VK_EXT_attachment_feedback_loop_layout`: read where the device lists it, and enabled with
|
||||
/// the other attachment-read features alone.
|
||||
pub feedback_loop: vk::PhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT<'static>,
|
||||
/// `VkPhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesEXT`, of
|
||||
/// `VK_EXT_attachment_feedback_loop_dynamic_state`: likewise.
|
||||
pub feedback_loop_dynamic:
|
||||
vk::PhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesEXT<'static>,
|
||||
}
|
||||
|
||||
/// One feature: the Vulkan structure that reports it, its member as ash spells it, the member, and
|
||||
@@ -211,6 +229,12 @@ macro_rules! feature {
|
||||
(@structure portability) => {
|
||||
"VkPhysicalDevicePortabilitySubsetFeaturesKHR"
|
||||
};
|
||||
(@structure feedback_loop) => {
|
||||
"VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT"
|
||||
};
|
||||
(@structure feedback_loop_dynamic) => {
|
||||
"VkPhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesEXT"
|
||||
};
|
||||
(@translator) => {
|
||||
None
|
||||
};
|
||||
@@ -317,6 +341,14 @@ pub(crate) const OPTIONAL: &[Feature] = &[
|
||||
feature!(subgroup_uniform_control_flow.shader_subgroup_uniform_control_flow => ShaderSubgroupUniformControlFlow),
|
||||
];
|
||||
|
||||
/// The features of a draw that reads an attachment of its own pass through a texture
|
||||
/// ([`ATTACHMENT_READ_EXTENSIONS`]): enabled together where the device reports both, and neither
|
||||
/// otherwise; a device without them refuses such a draw.
|
||||
pub(crate) const ATTACHMENT_READS: &[Feature] = &[
|
||||
feature!(feedback_loop.attachment_feedback_loop_layout),
|
||||
feature!(feedback_loop_dynamic.attachment_feedback_loop_dynamic_state),
|
||||
];
|
||||
|
||||
/// What the translator's shaders may use of what device creation enables: each enabled feature of
|
||||
/// `capabilities` the translator names, and each enabled extension it names.
|
||||
pub(crate) fn shader_enables(capabilities: &Capabilities) -> EnableSet {
|
||||
@@ -488,11 +520,15 @@ pub struct Capabilities {
|
||||
/// `shaderSubgroupUniformControlFlow`: SIMD groups reconverge where control flow is uniform,
|
||||
/// the mode where maximal reconvergence is not offered.
|
||||
pub subgroup_uniform_control_flow: bool,
|
||||
/// Every attachment-read feature, with [`ATTACHMENT_READ_EXTENSIONS`]: a draw may read,
|
||||
/// through a texture, an attachment its own pass renders, as Metal reads a render target.
|
||||
pub attachment_reads: bool,
|
||||
/// The features device creation enables: every one the floor requires, and each of the
|
||||
/// optional ones the device reports.
|
||||
pub features: Features,
|
||||
/// The device extensions device creation enables: [`DEVICE_EXTENSIONS`], then each of
|
||||
/// [`OPTIONAL_EXTENSIONS`] the device lists.
|
||||
/// [`OPTIONAL_EXTENSIONS`] the device lists, then [`ATTACHMENT_READ_EXTENSIONS`] where
|
||||
/// [`attachment_reads`](Self::attachment_reads) holds.
|
||||
pub extensions: Vec<&'static CStr>,
|
||||
/// The first queue family with graphics and compute, whose timestamps the floor checked.
|
||||
pub queue_family: u32,
|
||||
@@ -592,6 +628,9 @@ unsafe fn query(
|
||||
listed(ash::khr::shader_subgroup_uniform_control_flow::NAME);
|
||||
let robustness2_listed = listed(ash::ext::robustness2::NAME);
|
||||
let portability_listed = listed(ash::khr::portability_subset::NAME);
|
||||
let feedback_loop_listed = listed(ash::ext::attachment_feedback_loop_layout::NAME);
|
||||
let feedback_loop_dynamic_listed =
|
||||
listed(ash::ext::attachment_feedback_loop_dynamic_state::NAME);
|
||||
|
||||
let Features {
|
||||
core,
|
||||
@@ -602,6 +641,8 @@ unsafe fn query(
|
||||
subgroup_uniform_control_flow,
|
||||
robustness2,
|
||||
portability,
|
||||
feedback_loop,
|
||||
feedback_loop_dynamic,
|
||||
} = &mut queried.features;
|
||||
let mut features = vk::PhysicalDeviceFeatures2::default()
|
||||
.push_next(&mut *vulkan11)
|
||||
@@ -619,6 +660,12 @@ unsafe fn query(
|
||||
if portability_listed {
|
||||
features = features.push_next(&mut *portability);
|
||||
}
|
||||
if feedback_loop_listed {
|
||||
features = features.push_next(&mut *feedback_loop);
|
||||
}
|
||||
if feedback_loop_dynamic_listed {
|
||||
features = features.push_next(&mut *feedback_loop_dynamic);
|
||||
}
|
||||
instance.get_physical_device_features2(physical, &mut features);
|
||||
*core = features.features;
|
||||
vulkan11.p_next = std::ptr::null_mut();
|
||||
@@ -628,6 +675,8 @@ unsafe fn query(
|
||||
subgroup_uniform_control_flow.p_next = std::ptr::null_mut();
|
||||
robustness2.p_next = std::ptr::null_mut();
|
||||
portability.p_next = std::ptr::null_mut();
|
||||
feedback_loop.p_next = std::ptr::null_mut();
|
||||
feedback_loop_dynamic.p_next = std::ptr::null_mut();
|
||||
|
||||
queried.families = instance.get_physical_device_queue_family_properties(physical);
|
||||
queried.memory = instance.get_physical_device_memory_properties(physical);
|
||||
@@ -715,6 +764,16 @@ pub(crate) fn decide(device: &Queried) -> Result<Capabilities, BelowFloor> {
|
||||
for feature in OPTIONAL {
|
||||
*(feature.field)(&mut features) = *(feature.field)(&mut reported);
|
||||
}
|
||||
let listed =
|
||||
|extension: &CStr| (device.extensions.iter()).any(|name| name.as_c_str() == extension);
|
||||
let attachment_reads = ATTACHMENT_READ_EXTENSIONS.iter().all(|&name| listed(name))
|
||||
&& (ATTACHMENT_READS.iter()).all(|feature| *(feature.field)(&mut reported) == vk::TRUE)
|
||||
&& feedback_room(device);
|
||||
if attachment_reads {
|
||||
for feature in ATTACHMENT_READS {
|
||||
*(feature.field)(&mut features) = vk::TRUE;
|
||||
}
|
||||
}
|
||||
Ok(Capabilities {
|
||||
float16: features.vulkan12.shader_float16 == vk::TRUE,
|
||||
storage_read_without_format: features.core.shader_storage_image_read_without_format
|
||||
@@ -728,15 +787,20 @@ pub(crate) fn decide(device: &Queried) -> Result<Capabilities, BelowFloor> {
|
||||
.subgroup_uniform_control_flow
|
||||
.shader_subgroup_uniform_control_flow
|
||||
== vk::TRUE,
|
||||
attachment_reads,
|
||||
features,
|
||||
extensions: DEVICE_EXTENSIONS
|
||||
.iter()
|
||||
.chain(OPTIONAL_EXTENSIONS.iter().filter(|&&extension| {
|
||||
device
|
||||
.extensions
|
||||
.chain(
|
||||
OPTIONAL_EXTENSIONS
|
||||
.iter()
|
||||
.any(|name| name.as_c_str() == extension)
|
||||
}))
|
||||
.filter(|&&extension| listed(extension)),
|
||||
)
|
||||
.chain(
|
||||
ATTACHMENT_READ_EXTENSIONS
|
||||
.iter()
|
||||
.filter(|_| attachment_reads),
|
||||
)
|
||||
.copied()
|
||||
.collect(),
|
||||
queue_family,
|
||||
@@ -825,6 +889,21 @@ pub(crate) fn flag_names(flags: vk::MemoryPropertyFlags) -> String {
|
||||
.join(" | ")
|
||||
}
|
||||
|
||||
/// Whether the device's descriptor limits hold the sampled-texture array's feedback elements
|
||||
/// (`arrays::sampled_images`) beside the floor's arrays: what the attachment reads take beyond
|
||||
/// [`limits`].
|
||||
fn feedback_room(device: &Queried) -> bool {
|
||||
let limits = &device.limits;
|
||||
let descriptors = &device.vulkan12;
|
||||
let sampled = 2 * SAMPLED_IMAGES;
|
||||
descriptors.max_per_stage_descriptor_update_after_bind_sampled_images >= sampled
|
||||
&& descriptors.max_descriptor_set_update_after_bind_sampled_images >= sampled
|
||||
&& descriptors.max_per_stage_update_after_bind_resources
|
||||
>= sampled + STORAGE_IMAGES + limits.max_color_attachments
|
||||
&& descriptors.max_update_after_bind_descriptors_in_all_pools
|
||||
>= sampled + STORAGE_IMAGES + SAMPLER_ARRAYS * SAMPLERS
|
||||
}
|
||||
|
||||
/// Each limit the floor sets: its Vulkan name, the device's value, and the least value the runtime
|
||||
/// uses.
|
||||
pub(crate) fn limits(device: &Queried) -> [(&'static str, u32, u32); 15] {
|
||||
|
||||
@@ -217,7 +217,8 @@ pub(crate) struct Target {
|
||||
pub(crate) texture: Texture,
|
||||
pub(crate) attachment: Attachment,
|
||||
/// The layout the pass renders it in: its home, or the attachment layout for an image whose
|
||||
/// home is shader-read-only, which the pass moves there and back.
|
||||
/// home is shader-read-only, which the pass moves there and back; the feedback layout once a
|
||||
/// draw of the pass read it through a texture (`crate::render_encoder`).
|
||||
pub(crate) layout: vk::ImageLayout,
|
||||
load: vk::AttachmentLoadOp,
|
||||
store: vk::AttachmentStoreOp,
|
||||
@@ -252,24 +253,41 @@ impl Target {
|
||||
new: vk::ImageLayout,
|
||||
) -> vk::ImageMemoryBarrier2<'static> {
|
||||
let a = &self.attachment;
|
||||
vk::ImageMemoryBarrier2::default()
|
||||
.src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS)
|
||||
.src_access_mask(vk::AccessFlags2::MEMORY_WRITE)
|
||||
.dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS)
|
||||
.dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE)
|
||||
.old_layout(old)
|
||||
.new_layout(new)
|
||||
.src_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
|
||||
.dst_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
|
||||
.image(a.image)
|
||||
.subresource_range(vk::ImageSubresourceRange {
|
||||
aspect_mask: a.aspects,
|
||||
base_mip_level: a.level,
|
||||
level_count: 1,
|
||||
base_array_layer: a.layer,
|
||||
layer_count: 1,
|
||||
})
|
||||
layout_barrier(a.image, self.range(), old, new)
|
||||
}
|
||||
|
||||
/// Its level and layer, every aspect.
|
||||
pub(crate) fn range(&self) -> vk::ImageSubresourceRange {
|
||||
let a = &self.attachment;
|
||||
vk::ImageSubresourceRange {
|
||||
aspect_mask: a.aspects,
|
||||
base_mip_level: a.level,
|
||||
level_count: 1,
|
||||
base_array_layer: a.layer,
|
||||
layer_count: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The barrier that moves `range` of `image` from `old` to `new`, after every earlier command and
|
||||
/// before every later one.
|
||||
pub(crate) fn layout_barrier(
|
||||
image: vk::Image,
|
||||
range: vk::ImageSubresourceRange,
|
||||
old: vk::ImageLayout,
|
||||
new: vk::ImageLayout,
|
||||
) -> vk::ImageMemoryBarrier2<'static> {
|
||||
vk::ImageMemoryBarrier2::default()
|
||||
.src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS)
|
||||
.src_access_mask(vk::AccessFlags2::MEMORY_WRITE)
|
||||
.dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS)
|
||||
.dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE)
|
||||
.old_layout(old)
|
||||
.new_layout(new)
|
||||
.src_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
|
||||
.dst_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
|
||||
.image(image)
|
||||
.subresource_range(range)
|
||||
}
|
||||
|
||||
/// A pass descriptor checked against its textures: its attachments, by index for the colors,
|
||||
|
||||
@@ -729,7 +729,13 @@ impl RenderCompiled {
|
||||
.alpha_to_coverage_enable(fixed.alpha_to_coverage);
|
||||
let depth_stencil = vk::PipelineDepthStencilStateCreateInfo::default();
|
||||
let blend = vk::PipelineColorBlendStateCreateInfo::default().attachments(&fixed.blends);
|
||||
let dynamic_states = vk::PipelineDynamicStateCreateInfo::default().dynamic_states(&DYNAMIC);
|
||||
// Where the device reads attachments through textures, every pipeline takes the feedback
|
||||
// loop's enable as dynamic state too: a state is made before any draw binds its textures.
|
||||
let feedback = core.feedback.is_some();
|
||||
let states: Vec<vk::DynamicState> = (DYNAMIC.iter().copied())
|
||||
.chain(feedback.then_some(vk::DynamicState::ATTACHMENT_FEEDBACK_LOOP_ENABLE_EXT))
|
||||
.collect();
|
||||
let dynamic_states = vk::PipelineDynamicStateCreateInfo::default().dynamic_states(&states);
|
||||
let mut rendering = vk::PipelineRenderingCreateInfo::default()
|
||||
.color_attachment_formats(&fixed.colors)
|
||||
.stencil_attachment_format(fixed.stencil);
|
||||
|
||||
@@ -16,6 +16,24 @@
|
||||
//! buffer's device copy, `crate::over`), recording the copy between. A memory barrier after no
|
||||
//! such draw records nothing: the pass's first barrier ordered everything before it.
|
||||
//!
|
||||
//! A draw may read an attachment of its own pass through a texture, as Metal reads a render target
|
||||
//! (`floor::Capabilities::attachment_reads`): through the attachment feedback loop. Its image is a
|
||||
//! shader-read colour render target with no storage view, whose home is shader-read-only. At the
|
||||
//! pass's first draw that reads it, the rendering ends, one barrier moves the whole image to the
|
||||
//! feedback layout (its rendered level and layer from the attachment layout, the rest from home),
|
||||
//! and the rendering begins again with the attachment in that layout; the pass's second barrier
|
||||
//! moves the whole image home. While it is there, every draw of the pass reads the image's
|
||||
//! textures through their elements in that layout (`crate::arrays`). Every pipeline takes the
|
||||
//! feedback loop's enable as dynamic state, which the encoder sets at its first draw and where it
|
||||
//! changes: colour for a draw that reads an attachment, none otherwise.
|
||||
//!
|
||||
//! What such a draw reads that an earlier draw of the pass rendered, Metal orders by a memory
|
||||
//! barrier between the two. A memory barrier after draws that wrote no memory, where the pass
|
||||
//! moved an image, is owed to the next draw that reads an attachment: the rendering ends, one
|
||||
//! global barrier is recorded and it begins again, which orders every pixel, the draw's own and
|
||||
//! any other it reads. A move orders everything before it, so a barrier before the pass's first
|
||||
//! move records nothing.
|
||||
//!
|
||||
//! A draw sets Metal's state as dynamic state: every state at the encoder's first draw, Metal's
|
||||
//! defaults where the encoder set none, and after that what changed. A viewport is Vulkan's with
|
||||
//! its height negated (from `y + height`), so framebuffer rows, the fragment position and
|
||||
@@ -32,7 +50,7 @@ use crate::error::{DrawKind, Error, Gap, Limit, Refusal};
|
||||
use crate::fence::Fence;
|
||||
use crate::hazard::Access;
|
||||
use crate::over;
|
||||
use crate::pass::{Pass, RenderPassDescriptor};
|
||||
use crate::pass::{layout_barrier, Pass, RenderPassDescriptor};
|
||||
use crate::pipeline::{FieldValue, PUSH_STAGES};
|
||||
use crate::record::{memory_barrier, Reach};
|
||||
use crate::render::{RenderPipelineState, FRAGMENT, VERTEX};
|
||||
@@ -89,8 +107,9 @@ pub struct IndexBuffer<'a> {
|
||||
}
|
||||
|
||||
/// The dynamic state a draw records before it: the viewport and scissor, the cull mode, the
|
||||
/// front face, the depth and stencil tests, the stencil references, the blend constants, and the
|
||||
/// depth bias the encoder does not set.
|
||||
/// front face, the depth and stencil tests, the stencil references, the blend constants, the
|
||||
/// depth bias the encoder does not set, and the feedback loop's enable where the device reads
|
||||
/// attachments.
|
||||
const VIEWPORT: u32 = 1;
|
||||
const CULL: u32 = 2;
|
||||
const FRONT: u32 = 4;
|
||||
@@ -98,7 +117,27 @@ const TESTS: u32 = 8;
|
||||
const REFERENCE: u32 = 16;
|
||||
const BLEND: u32 = 32;
|
||||
const BIAS: u32 = 64;
|
||||
const ALL: u32 = 127;
|
||||
const LOOP: u32 = 128;
|
||||
const ALL: u32 = 255;
|
||||
|
||||
/// An image the pass moved whole to the feedback layout, which its second barrier moves home.
|
||||
struct Moved {
|
||||
/// Its textures' hazard id.
|
||||
hazard: u32,
|
||||
image: vk::Image,
|
||||
/// Every level and layer of it.
|
||||
whole: vk::ImageSubresourceRange,
|
||||
home: vk::ImageLayout,
|
||||
}
|
||||
|
||||
/// An image a draw moves to the feedback layout: its attachment's level and layer from the layout
|
||||
/// the pass renders it in, the rest of it from home.
|
||||
struct Move {
|
||||
moved: Moved,
|
||||
/// The attachment's level and layer.
|
||||
rendered: vk::ImageSubresourceRange,
|
||||
from: vk::ImageLayout,
|
||||
}
|
||||
|
||||
/// What a buffer slot holds.
|
||||
#[derive(Clone, Default)]
|
||||
@@ -217,6 +256,15 @@ pub struct RenderCommandEncoder {
|
||||
topology: Option<(vk::PrimitiveTopology, bool)>,
|
||||
/// A draw since the rendering began wrote memory: a memory barrier ends the rendering.
|
||||
wrote: bool,
|
||||
/// A draw since the encoder last ordered its draws rendered.
|
||||
drew: bool,
|
||||
/// A memory barrier after draws that wrote no memory, which the next draw that reads an
|
||||
/// attachment pays.
|
||||
owed: bool,
|
||||
/// The images the pass moved to the feedback layout.
|
||||
moved: Vec<Moved>,
|
||||
/// The feedback loop's aspects the last draw set: colour for a draw that reads an attachment.
|
||||
loop_aspects: vk::ImageAspectFlags,
|
||||
/// The fixed-function vertex buffers of the draw being recorded: binding, buffer and offset.
|
||||
vertex_buffers: Vec<(u32, vk::Buffer, u64)>,
|
||||
/// The push bytes of the draw being recorded.
|
||||
@@ -270,6 +318,10 @@ impl CommandBuffer {
|
||||
dirty: ALL,
|
||||
topology: None,
|
||||
wrote: false,
|
||||
drew: false,
|
||||
owed: false,
|
||||
moved: Vec::new(),
|
||||
loop_aspects: vk::ImageAspectFlags::NONE,
|
||||
vertex_buffers: Vec::new(),
|
||||
push: Vec::new(),
|
||||
pass,
|
||||
@@ -669,14 +721,25 @@ impl RenderCommandEncoder {
|
||||
/// `memoryBarrierWithScope:afterStages:beforeStages:` and
|
||||
/// `memoryBarrierWithResources:count:afterStages:beforeStages:`: after a draw of the encoder
|
||||
/// that wrote memory, the rendering ends, one global memory barrier is recorded, and the
|
||||
/// rendering begins again, loading what it stored; after none, nothing is recorded, as the
|
||||
/// pass's first barrier ordered everything before it.
|
||||
/// rendering begins again, loading what it stored. After draws that only rendered, where the
|
||||
/// pass moved an image to the feedback layout, the barrier is owed to the next draw that reads
|
||||
/// an attachment through a texture, which records it first (the module's documentation);
|
||||
/// otherwise nothing is recorded, as the pass's first barrier ordered everything before it and
|
||||
/// no draw reads an attachment before a move that orders everything before it.
|
||||
///
|
||||
/// Err: [`Error::Vulkan`] where the command buffer cannot record.
|
||||
pub fn memory_barrier(&mut self) -> Result<(), Error> {
|
||||
if !self.wrote {
|
||||
return Ok(());
|
||||
if self.wrote {
|
||||
return self.ordered();
|
||||
}
|
||||
self.owed |= self.drew && !self.moved.is_empty();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ends the rendering, records one global memory barrier and begins the rendering again: the
|
||||
/// encoder's draws so far are ordered before its later ones.
|
||||
fn ordered(&mut self) -> Result<(), Error> {
|
||||
(self.drew, self.owed) = (false, false);
|
||||
self.interrupt(|building| {
|
||||
let command = building.ordered_command()?;
|
||||
building.barrier(command, Reach::Commands);
|
||||
@@ -684,6 +747,48 @@ impl RenderCommandEncoder {
|
||||
})
|
||||
}
|
||||
|
||||
/// Moves each image of `moves` whole to the feedback layout: the rendering ends, one barrier
|
||||
/// orders every earlier command before every later one and moves them, and the rendering
|
||||
/// begins again with their attachments in that layout.
|
||||
fn feedback_moves(&mut self, moves: Vec<Move>) -> Result<(), Error> {
|
||||
let looped = vk::ImageLayout::ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT;
|
||||
let mut images = Vec::new();
|
||||
for m in &moves {
|
||||
let Moved {
|
||||
image, whole, home, ..
|
||||
} = m.moved;
|
||||
images.push(layout_barrier(image, m.rendered, m.from, looped));
|
||||
for range in around(whole, m.rendered) {
|
||||
images.push(layout_barrier(image, range, home, looped));
|
||||
}
|
||||
}
|
||||
for t in self.pass.colors.iter_mut().flatten() {
|
||||
if (moves.iter()).any(|m| m.moved.hazard == t.texture.hazard_id()) {
|
||||
t.layout = looped;
|
||||
}
|
||||
}
|
||||
self.moved.extend(moves.into_iter().map(|m| m.moved));
|
||||
(self.drew, self.owed) = (false, false);
|
||||
self.interrupt(|building| {
|
||||
let command = building.ordered_command()?;
|
||||
let core = Arc::clone(building.device.core());
|
||||
let memory = [memory_barrier(Reach::Commands)];
|
||||
// SAFETY: the command buffer records outside any rendering; each range is in the
|
||||
// layout it moves from: the attachment's in the layout the pass rendered it in, the
|
||||
// rest of the image at home.
|
||||
unsafe {
|
||||
core.device.cmd_pipeline_barrier2(
|
||||
command,
|
||||
&vk::DependencyInfo::default()
|
||||
.memory_barriers(&memory)
|
||||
.image_memory_barriers(&images),
|
||||
)
|
||||
};
|
||||
core.counters.count(Metric::Barriers);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// `updateFence:afterStages:`: encoders that wait on `fence` after this one see what this
|
||||
/// encoder and the ones before it wrote. It records nothing ([`Fence`]): the pass's second
|
||||
/// barrier already reaches them.
|
||||
@@ -851,16 +956,51 @@ impl RenderCommandEncoder {
|
||||
};
|
||||
self.vertex_buffers.push((binding, buffer, offset));
|
||||
}
|
||||
// A texture the pass renders into, read or written as the pass renders it, is a feedback
|
||||
// loop the runtime does not build.
|
||||
// A texture whose image the pass renders. One that reaches the attachment's level and
|
||||
// layer reads the attachment: from the fragment function, never written, of an image of
|
||||
// the feedback usage that one attachment of the pass renders. One that reaches neither
|
||||
// reads its image at home, or, once the pass moved the image, in the feedback layout,
|
||||
// never written. The pass's first draw that reads an attachment moves its image.
|
||||
let tables = &self.tables;
|
||||
let rendered = (draws.textures.iter()).any(|&(table, slot, _)| {
|
||||
tables[table].texture(slot).is_some_and(|texture| {
|
||||
(self.pass.targets()).any(|t| t.texture.hazard_id() == texture.hazard_id())
|
||||
})
|
||||
});
|
||||
if rendered {
|
||||
return gap(DrawKind::AttachmentRead);
|
||||
let mut reads = false;
|
||||
let mut moves: Vec<Move> = Vec::new();
|
||||
for &(table, slot, access) in &draws.textures {
|
||||
let Some(texture) = tables[table].texture(slot) else {
|
||||
continue;
|
||||
};
|
||||
let hazard = texture.hazard_id();
|
||||
let mut rendered = (self.pass.targets()).filter(|t| t.texture.hazard_id() == hazard);
|
||||
let Some(target) = rendered.next() else {
|
||||
continue;
|
||||
};
|
||||
let a = &target.attachment;
|
||||
let reaches = texture.reaches(a.level, a.layer);
|
||||
let moved = (self.moved.iter()).any(|m| m.hazard == hazard)
|
||||
|| (moves.iter()).any(|m| m.moved.hazard == hazard);
|
||||
if !reaches && !moved {
|
||||
continue;
|
||||
}
|
||||
let readable = core.feedback.is_some()
|
||||
&& texture.feedback()
|
||||
&& !access.writes()
|
||||
&& (table == FRAGMENT || !reaches)
|
||||
&& rendered.next().is_none();
|
||||
if !readable {
|
||||
return gap(DrawKind::AttachmentRead);
|
||||
}
|
||||
reads |= reaches;
|
||||
if !moved {
|
||||
moves.push(Move {
|
||||
moved: Moved {
|
||||
hazard,
|
||||
image: a.image,
|
||||
whole: texture.whole(),
|
||||
home: a.home,
|
||||
},
|
||||
rendered: target.range(),
|
||||
from: target.layout,
|
||||
});
|
||||
}
|
||||
}
|
||||
for &(_, table, value) in &draws.fields {
|
||||
if let FieldValue::BufferAddress { slot, mask } = value {
|
||||
@@ -879,6 +1019,24 @@ impl RenderCommandEncoder {
|
||||
return Ok(());
|
||||
}
|
||||
let pipeline = compiled.pipeline(topology)?;
|
||||
// The attachments the draw reads: their images moved, which orders everything before, or
|
||||
// the barrier owed paid.
|
||||
if !moves.is_empty() {
|
||||
self.feedback_moves(moves)?;
|
||||
} else if reads && self.owed {
|
||||
self.ordered()?;
|
||||
}
|
||||
if core.feedback.is_some() {
|
||||
let aspects = match reads {
|
||||
true => vk::ImageAspectFlags::COLOR,
|
||||
false => vk::ImageAspectFlags::NONE,
|
||||
};
|
||||
if aspects != self.loop_aspects {
|
||||
self.loop_aspects = aspects;
|
||||
self.dirty |= LOOP;
|
||||
}
|
||||
}
|
||||
let tables = &self.tables;
|
||||
// The copies the draw needs first: its textures over a buffer in an image of their own
|
||||
// (`crate::over`), and the textures over a Managed buffer's device copy it reaches
|
||||
// (`crate::over::ManagedOvers`), each recorded between the rendering's end and its new
|
||||
@@ -951,6 +1109,7 @@ impl RenderCommandEncoder {
|
||||
building.wrote_host();
|
||||
}
|
||||
self.wrote |= draws.writes_memory;
|
||||
self.drew = true;
|
||||
let (bind, first) = building.bind_graphics(pipeline);
|
||||
self.fill_push(&state);
|
||||
let device = &core.device;
|
||||
@@ -974,7 +1133,7 @@ impl RenderCommandEncoder {
|
||||
if bind {
|
||||
device.cmd_bind_pipeline(command, vk::PipelineBindPoint::GRAPHICS, pipeline);
|
||||
}
|
||||
self.record_state(device, topology, restart);
|
||||
self.record_state(device, core.feedback.as_ref(), topology, restart);
|
||||
for &(binding, buffer, offset) in &self.vertex_buffers {
|
||||
device.cmd_bind_vertex_buffers(command, binding, &[buffer], &[offset]);
|
||||
}
|
||||
@@ -1035,8 +1194,15 @@ impl RenderCommandEncoder {
|
||||
let address = tables.address(slot);
|
||||
push[at..at + 8].copy_from_slice(&address.to_le_bytes());
|
||||
}
|
||||
// A texture whose image the pass moved to the feedback layout reads through its
|
||||
// element in that layout.
|
||||
FieldValue::TextureId(slot) => {
|
||||
let id = (tables.texture(slot)).map_or(0, |t| t.gpu_resource_id().to_bits());
|
||||
let id = (tables.texture(slot)).map_or(0, |t| {
|
||||
match (self.moved.iter()).any(|m| m.hazard == t.hazard_id()) {
|
||||
true => t.feedback_resource_id().to_bits(),
|
||||
false => t.gpu_resource_id().to_bits(),
|
||||
}
|
||||
});
|
||||
push[at..at + 8].copy_from_slice(&id.to_le_bytes());
|
||||
}
|
||||
FieldValue::SamplerId(slot) => {
|
||||
@@ -1070,6 +1236,7 @@ impl RenderCommandEncoder {
|
||||
unsafe fn record_state(
|
||||
&mut self,
|
||||
device: &ash::Device,
|
||||
feedback: Option<&ash::ext::attachment_feedback_loop_dynamic_state::Device>,
|
||||
topology: vk::PrimitiveTopology,
|
||||
restart: bool,
|
||||
) {
|
||||
@@ -1130,6 +1297,13 @@ impl RenderCommandEncoder {
|
||||
device.cmd_set_depth_bias(command, 0.0, 0.0, 0.0);
|
||||
device.cmd_set_depth_bias_enable(command, false);
|
||||
}
|
||||
// Every pipeline takes the enable as dynamic state where the device reads attachments.
|
||||
if let (true, Some(feedback)) = (dirty & LOOP != 0, feedback) {
|
||||
(feedback.fp().cmd_set_attachment_feedback_loop_enable_ext)(
|
||||
command,
|
||||
self.loop_aspects,
|
||||
);
|
||||
}
|
||||
if self.topology != Some((topology, restart)) {
|
||||
device.cmd_set_primitive_topology(command, topology);
|
||||
device.cmd_set_primitive_restart_enable(command, restart);
|
||||
@@ -1193,9 +1367,16 @@ impl RenderCommandEncoder {
|
||||
};
|
||||
let core = Arc::clone(building.device.core());
|
||||
let later = [memory_barrier(Reach::Commands)];
|
||||
let looped = vk::ImageLayout::ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT;
|
||||
let moved = &self.moved;
|
||||
let back: Vec<vk::ImageMemoryBarrier2> = (self.pass.targets())
|
||||
.filter(|t| t.moves())
|
||||
.filter(|t| t.moves() && !moved.iter().any(|m| m.hazard == t.texture.hazard_id()))
|
||||
.map(|t| t.barrier(t.layout, t.attachment.home))
|
||||
.chain(
|
||||
moved
|
||||
.iter()
|
||||
.map(|m| layout_barrier(m.image, m.whole, looped, m.home)),
|
||||
)
|
||||
.collect();
|
||||
let device = &core.device;
|
||||
// SAFETY: the command buffer is inside the pass's rendering, which nothing else records
|
||||
@@ -1221,6 +1402,39 @@ impl Drop for RenderCommandEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
/// `whole` but `rendered`, one level and layer of it: at most four ranges.
|
||||
fn around(
|
||||
whole: vk::ImageSubresourceRange,
|
||||
rendered: vk::ImageSubresourceRange,
|
||||
) -> Vec<vk::ImageSubresourceRange> {
|
||||
let (level, layer) = (rendered.base_mip_level, rendered.base_array_layer);
|
||||
let levels = |first: u32, end: u32| {
|
||||
(first < end).then_some(vk::ImageSubresourceRange {
|
||||
base_mip_level: first,
|
||||
level_count: end - first,
|
||||
..whole
|
||||
})
|
||||
};
|
||||
let layers = |first: u32, end: u32| {
|
||||
(first < end).then_some(vk::ImageSubresourceRange {
|
||||
base_mip_level: level,
|
||||
level_count: 1,
|
||||
base_array_layer: first,
|
||||
layer_count: end - first,
|
||||
..whole
|
||||
})
|
||||
};
|
||||
[
|
||||
levels(whole.base_mip_level, level),
|
||||
levels(level + 1, whole.base_mip_level + whole.level_count),
|
||||
layers(whole.base_array_layer, layer),
|
||||
layers(layer + 1, whole.base_array_layer + whole.layer_count),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The slot of buffer index `index`.
|
||||
///
|
||||
/// Err: [`Refusal::BufferIndex`] for an index of 31 or more.
|
||||
|
||||
@@ -258,7 +258,7 @@ mod on_device {
|
||||
let core = device.core();
|
||||
let start = Instant::now();
|
||||
// SAFETY: the device is live and made at the floor.
|
||||
let second = unsafe { Arrays::new(&core.device, &counters) }
|
||||
let second = unsafe { Arrays::new(&core.device, &counters, false) }
|
||||
.unwrap_or_else(|error| panic!("{name}: a second set 1: {error}"));
|
||||
let micros = start.elapsed().as_micros();
|
||||
assert_eq!(counters.gauges().device_samplers, 4, "{name}");
|
||||
|
||||
@@ -955,6 +955,179 @@ mod on_device {
|
||||
});
|
||||
}
|
||||
|
||||
/// A shader-read render target of `format`, `SIDE` a side and `levels` levels, at `index`.
|
||||
fn readable(device: &Device, format: PixelFormat, levels: u32, index: u32) -> Texture {
|
||||
let mut d = TextureDescriptor::new(TextureType::Type2D, format, SIDE, SIDE);
|
||||
d.usage = TextureUsage::RENDER_TARGET | TextureUsage::SHADER_READ;
|
||||
d.storage_mode = StorageMode::Shared;
|
||||
d.mipmap_level_count = levels;
|
||||
(device.new_texture(&d, Some(index))).unwrap_or_else(|error| panic!("{format:?}: {error}"))
|
||||
}
|
||||
|
||||
/// A draw reads, through a texture, the attachment its own pass renders, as Metal reads a
|
||||
/// render target: after a memory barrier, what an earlier draw of the pass rendered, at the
|
||||
/// fragment's own pixel and at another pixel, the rendering ended and begun again around one
|
||||
/// barrier. The pass's first such draw moves the image to the feedback layout, a barrier of
|
||||
/// its own. A device without the attachment reads refuses the draw,
|
||||
/// `Gap::Draw { AttachmentRead }`, and records nothing for it.
|
||||
#[test]
|
||||
fn every_device_reads_its_attachment_through_a_texture_after_a_memory_barrier() {
|
||||
let Some(library) = examples() else {
|
||||
return;
|
||||
};
|
||||
let plain = descriptor(&library, "colored_fragment", PixelFormat::RGBA8Unorm);
|
||||
let own = descriptor(&library, "own_pixel_fragment", PixelFormat::RGBA8Unorm);
|
||||
let first = descriptor(&library, "first_texel_fragment", PixelFormat::RGBA8Unorm);
|
||||
each_device("attachment_reads", |name, _, device| {
|
||||
(device.grant_indices(IndexClass::Texture, 4096, 4))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
let reads = device.core().feedback.is_some();
|
||||
let plain = state(name, &device, &plain);
|
||||
let mut buffers = Buffers::new(&device);
|
||||
// Three triangles over the whole target, their colors none, blue and green.
|
||||
let whole = [-1.0, -1.0, 3.0, -1.0, -1.0, 3.0];
|
||||
let positions = buffers.of(&device, &floats(&[whole; 3].concat()));
|
||||
let colors: Vec<f32> = [[0.0; 4], BLUE, [0.0, 1.0, 0.0, 0.0]]
|
||||
.iter()
|
||||
.flat_map(|c| [*c; 3].concat())
|
||||
.collect();
|
||||
let colors = buffers.of(&device, &floats(&colors));
|
||||
let queue = device.new_command_queue(4).expect("a queue");
|
||||
for (case, (reading, index)) in
|
||||
[("own pixel", (&own, 4096)), ("first texel", (&first, 4097))]
|
||||
{
|
||||
let reading = state(name, &device, reading);
|
||||
let color = readable(&device, PixelFormat::RGBA8Unorm, 1, index);
|
||||
// The upload that moves the new target to its home layout, in a submission apart.
|
||||
let upload = queue.command_buffer().expect("a command buffer");
|
||||
(upload.record_for_test(&[], |_, _| {})).expect("a command buffer's commands");
|
||||
run(name, &upload);
|
||||
let counters = device.counters();
|
||||
settled(&device);
|
||||
let before = counters.metrics();
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
let mut encoder = (command_buffer.render_command_encoder(&pass(&color, None)))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
let draw = |result: Result<(), Error>| {
|
||||
result.unwrap_or_else(|e| panic!("{name} {case}: {e}"))
|
||||
};
|
||||
draw(encoder.set_vertex_buffer(Some(&positions), 0, 0));
|
||||
draw(encoder.set_vertex_buffer(Some(&colors), 0, 1));
|
||||
draw(encoder.set_fragment_texture(Some(&color), 0));
|
||||
draw(encoder.set_render_pipeline_state(&reading));
|
||||
// The pass's first read of its attachment: the clear's black, and none added.
|
||||
let moved = encoder.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0);
|
||||
if !reads {
|
||||
assert_eq!(
|
||||
format!("{moved:?}"),
|
||||
format!(
|
||||
"{:?}",
|
||||
Err::<(), _>(Error::Unsupported(Gap::Draw {
|
||||
kind: DrawKind::AttachmentRead
|
||||
}))
|
||||
),
|
||||
"{name} {case}: a device without the attachment reads"
|
||||
);
|
||||
encoder.end_encoding();
|
||||
run(name, &command_buffer);
|
||||
continue;
|
||||
}
|
||||
draw(moved);
|
||||
// Blue over every pixel, then the barrier, then each pixel read plus green.
|
||||
draw(encoder.set_render_pipeline_state(&plain));
|
||||
draw(encoder.draw_primitives(PrimitiveType::Triangle, 3, 3, 1, 0));
|
||||
draw(encoder.memory_barrier());
|
||||
draw(encoder.set_render_pipeline_state(&reading));
|
||||
draw(encoder.draw_primitives(PrimitiveType::Triangle, 6, 3, 1, 0));
|
||||
encoder.end_encoding();
|
||||
run(name, &command_buffer);
|
||||
let after = counters.metrics();
|
||||
assert_eq!(
|
||||
(after.barriers - before.barriers, after.passes - before.passes),
|
||||
(5, 1),
|
||||
"{name} {case}: barriers (the pass's two, the move, the memory barrier's, the segment's end), passes"
|
||||
);
|
||||
let want = texels(|_, _| [0.0, 1.0, 1.0, 1.0]);
|
||||
assert_eq!(
|
||||
read(&color, 4),
|
||||
want,
|
||||
"{name} {case}: every pixel blue read plus green"
|
||||
);
|
||||
}
|
||||
match reads {
|
||||
true => println!("{name}: an attachment read after a memory barrier, at its own pixel and at another"),
|
||||
false => println!("{name}: no attachment reads; the reading draw refused"),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// A pass that renders a texture's second level reads its first through a view of both
|
||||
/// levels, which reaches the rendered one: the draw reads through the feedback loop, its
|
||||
/// whole image moved, and samples the level an earlier pass rendered.
|
||||
#[test]
|
||||
fn every_device_samples_a_lower_level_while_its_pass_renders_the_next() {
|
||||
let Some(library) = examples() else {
|
||||
return;
|
||||
};
|
||||
let d = descriptor(&library, "first_level_fragment", PixelFormat::RGBA8Unorm);
|
||||
each_device("attachment_levels", |name, _, device| {
|
||||
if device.core().feedback.is_none() {
|
||||
println!("{name}: no attachment reads");
|
||||
return;
|
||||
}
|
||||
for (class, first) in [(IndexClass::Texture, 4096), (IndexClass::Sampler, 1)] {
|
||||
(device.grant_indices(class, first, 4))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
}
|
||||
let state = (device.new_render_pipeline_state(&d, |_| Ok(vec![CONSTEXPR])))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
let chain = readable(&device, PixelFormat::RGBA8Unorm, 2, 4096);
|
||||
let mut buffers = Buffers::new(&device);
|
||||
let (positions, colors) = vertices(&device, &mut buffers);
|
||||
let queue = device.new_command_queue(4).expect("a queue");
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
// The first level cleared to blue by a pass of no draw.
|
||||
let mut first = pass(&chain, None);
|
||||
first.color_attachments[0].clear_color = crate::ClearColor::new(0.0, 0.0, 1.0, 1.0);
|
||||
(command_buffer.render_command_encoder(&first))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"))
|
||||
.end_encoding();
|
||||
let mut second = pass(&chain, None);
|
||||
second.color_attachments[0].level = 1;
|
||||
let mut encoder = (command_buffer.render_command_encoder(&second))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
let draw = |result: Result<(), Error>| result.unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
draw(encoder.set_render_pipeline_state(&state));
|
||||
draw(encoder.set_vertex_buffer(Some(&positions), 0, 0));
|
||||
draw(encoder.set_vertex_buffer(Some(&colors), 0, 1));
|
||||
draw(encoder.set_fragment_texture(Some(&chain), 0));
|
||||
draw(encoder.draw_primitives(PrimitiveType::Triangle, 3, 3, 1, 0));
|
||||
encoder.end_encoding();
|
||||
run(name, &command_buffer);
|
||||
let side = SIDE / 2;
|
||||
let mut bytes = vec![0; (side * side * 4) as usize];
|
||||
let region = Region {
|
||||
origin: [0; 3],
|
||||
size: [side, side, 1],
|
||||
};
|
||||
(chain.get_bytes(
|
||||
&mut bytes,
|
||||
u64::from(side * 4),
|
||||
u64::from(side * side * 4),
|
||||
region,
|
||||
1,
|
||||
0,
|
||||
))
|
||||
.expect("get_bytes");
|
||||
let want: Vec<u8> = (0..side * side).flat_map(|_| rgba8(BLUE)).collect();
|
||||
assert_eq!(
|
||||
bytes, want,
|
||||
"{name}: the second level, each texel the first's blue"
|
||||
);
|
||||
println!("{name}: a lower level sampled while its pass renders the next");
|
||||
});
|
||||
}
|
||||
|
||||
/// What Metal refuses is refused, and what a draw does not build is a gap.
|
||||
#[test]
|
||||
fn every_device_refuses_the_draws_it_does_not_build() {
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
use crate::floor::{
|
||||
choose, decide, flag_names, limits, vulkan_name, BelowFloor, Features, MemoryTypes, Missing,
|
||||
Queried, DEVICE_EXTENSIONS, INSTANCE_EXTENSIONS, MEMORY_FLAG_NAMES, OPTIONAL,
|
||||
OPTIONAL_EXTENSIONS, OPTIONAL_INSTANCE_EXTENSIONS, PORTABILITY, PRIVATE_MEMORY, REQUIRED,
|
||||
RING_MEMORY, SHARED_MEMORY, TIMESTAMP_BITS, TIME_DOMAINS, VERSION,
|
||||
Queried, ATTACHMENT_READS, ATTACHMENT_READ_EXTENSIONS, DEVICE_EXTENSIONS, INSTANCE_EXTENSIONS,
|
||||
MEMORY_FLAG_NAMES, OPTIONAL, OPTIONAL_EXTENSIONS, OPTIONAL_INSTANCE_EXTENSIONS, PORTABILITY,
|
||||
PRIVATE_MEMORY, REQUIRED, RING_MEMORY, SHARED_MEMORY, TIMESTAMP_BITS, TIME_DOMAINS, VERSION,
|
||||
};
|
||||
use ash::vk;
|
||||
use std::collections::BTreeSet;
|
||||
@@ -1029,6 +1029,11 @@ fn the_readme_states_the_floor() {
|
||||
.iter()
|
||||
.map(|name| (name, "device, where listed")),
|
||||
)
|
||||
.chain(
|
||||
ATTACHMENT_READ_EXTENSIONS
|
||||
.iter()
|
||||
.map(|name| (name, "device, with the attachment reads")),
|
||||
)
|
||||
.map(|(name, place)| (name.to_string_lossy().into_owned(), place.to_string()))
|
||||
.collect();
|
||||
assert_eq!(stated, code, "the README's extensions");
|
||||
@@ -1040,6 +1045,10 @@ fn the_readme_states_the_floor() {
|
||||
["Portability feature", "Structure", "Used for"],
|
||||
PORTABILITY,
|
||||
),
|
||||
(
|
||||
["Attachment-read feature", "Structure", "Used for"],
|
||||
ATTACHMENT_READS,
|
||||
),
|
||||
] {
|
||||
let stated: BTreeSet<(String, String)> = table(§ion, &header)
|
||||
.into_iter()
|
||||
@@ -1130,6 +1139,8 @@ fn every_device_the_loader_lists_is_above_the_floor() {
|
||||
mut subgroup_uniform_control_flow,
|
||||
mut robustness2,
|
||||
mut portability,
|
||||
mut feedback_loop,
|
||||
mut feedback_loop_dynamic,
|
||||
} = capabilities.features;
|
||||
let mut features = vk::PhysicalDeviceFeatures2::default()
|
||||
.features(core)
|
||||
@@ -1147,6 +1158,11 @@ fn every_device_the_loader_lists_is_above_the_floor() {
|
||||
if enabled(ash::khr::portability_subset::NAME) {
|
||||
features = features.push_next(&mut portability);
|
||||
}
|
||||
if capabilities.attachment_reads {
|
||||
features = features
|
||||
.push_next(&mut feedback_loop)
|
||||
.push_next(&mut feedback_loop_dynamic);
|
||||
}
|
||||
let extensions: Vec<*const c_char> = capabilities
|
||||
.extensions
|
||||
.iter()
|
||||
|
||||
@@ -285,6 +285,12 @@ pub(crate) struct Image {
|
||||
storage: Option<vk::ImageView>,
|
||||
/// The layout the image is in outside a copy, which its elements name.
|
||||
home: vk::ImageLayout,
|
||||
/// The image has the feedback usage (a shader-read colour render target of no storage view,
|
||||
/// its home shader-read-only, on a device that reads attachments so): each texture over it
|
||||
/// has a second element of the sampled-texture array, at its index plus
|
||||
/// [`SAMPLED_IMAGES`](crate::floor::SAMPLED_IMAGES), naming the feedback layout, which a pass
|
||||
/// moves the whole image to for a draw that reads an attachment of the pass through it.
|
||||
feedback: bool,
|
||||
/// Every aspect of the image's format, which a change of layout names.
|
||||
aspects: vk::ImageAspectFlags,
|
||||
/// The layout the image is made in: undefined, or preinitialized for a texture over a buffer
|
||||
@@ -418,8 +424,14 @@ impl Drop for TextureInner {
|
||||
// work that reads them is pending: each command buffer that used the texture held it
|
||||
// until it retired.
|
||||
unsafe {
|
||||
core.arrays
|
||||
.write_texture(&core.device, class, index, None, None)
|
||||
core.arrays.write_texture(
|
||||
&core.device,
|
||||
class,
|
||||
index,
|
||||
None,
|
||||
None,
|
||||
self.image.feedback,
|
||||
)
|
||||
};
|
||||
lock(&core.arrays.grants).vacate(class, index);
|
||||
core.counters.fall(Gauge::DescriptorSlots);
|
||||
@@ -770,6 +782,7 @@ impl Device {
|
||||
sampled,
|
||||
storage,
|
||||
home,
|
||||
feedback: plan.feedback && home == vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
|
||||
aspects: plan.aspects,
|
||||
first,
|
||||
homed: AtomicBool::new(false),
|
||||
@@ -806,6 +819,7 @@ impl Device {
|
||||
index,
|
||||
sampled.map(|view| (view, home)),
|
||||
storage,
|
||||
inner.image.feedback,
|
||||
)
|
||||
};
|
||||
}
|
||||
@@ -843,12 +857,22 @@ struct Plan {
|
||||
/// Whether the texture has a storage view.
|
||||
storage: bool,
|
||||
home: vk::ImageLayout,
|
||||
/// The image has the feedback usage, and each sampled view a second element ([`Image`]).
|
||||
feedback: bool,
|
||||
aspects: vk::ImageAspectFlags,
|
||||
unit: Option<CopyUnit>,
|
||||
word: u32,
|
||||
emulation: FormatEmulation,
|
||||
}
|
||||
|
||||
impl Plan {
|
||||
/// The usage of a linear image of the plan, over a buffer in place: without the feedback
|
||||
/// usage, as its home is general and no pass moves it to the feedback layout.
|
||||
fn linear_usage(&self) -> vk::ImageUsageFlags {
|
||||
self.usage & !vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT
|
||||
}
|
||||
}
|
||||
|
||||
/// The slices of each level of a texture of descriptor `d`: a cube's six faces each a slice, and
|
||||
/// one for a 3D texture.
|
||||
fn slices(d: &TextureDescriptor) -> u32 {
|
||||
@@ -993,7 +1017,7 @@ fn linear_extent(core: &Core, shape: &Shape, plan: &Plan) -> Option<vk::Extent3D
|
||||
.format(plan.format)
|
||||
.ty(shape.image_type)
|
||||
.tiling(vk::ImageTiling::LINEAR)
|
||||
.usage(plan.usage)
|
||||
.usage(plan.linear_usage())
|
||||
.flags(shape.flags);
|
||||
core.image_format_properties(&linear)
|
||||
.map(|properties| properties.max_extent)
|
||||
@@ -1025,6 +1049,7 @@ fn linear_layout(
|
||||
}
|
||||
let info = image_info(d, shape, plan)
|
||||
.tiling(vk::ImageTiling::LINEAR)
|
||||
.usage(plan.linear_usage())
|
||||
.initial_layout(vk::ImageLayout::PREINITIALIZED);
|
||||
let device = &core.device;
|
||||
// SAFETY: the device is live, and makes a linear image of the format, usage and extent.
|
||||
@@ -1175,6 +1200,16 @@ fn plan(
|
||||
false => vk::ImageUsageFlags::COLOR_ATTACHMENT,
|
||||
};
|
||||
}
|
||||
// A draw may read an attachment of its own pass through such a texture, in the feedback
|
||||
// layout (`crate::render_encoder`), where the device reads attachments so.
|
||||
let mut feedback = core.feedback.is_some()
|
||||
&& read
|
||||
&& render
|
||||
&& !storage
|
||||
&& aspects == vk::ImageAspectFlags::COLOR;
|
||||
if feedback {
|
||||
image_usage |= vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT;
|
||||
}
|
||||
let home = match (storage, read, render) {
|
||||
(true, _, _) => vk::ImageLayout::GENERAL,
|
||||
(false, true, _) => vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
|
||||
@@ -1188,7 +1223,15 @@ fn plan(
|
||||
.tiling(vk::ImageTiling::OPTIMAL)
|
||||
.usage(image_usage)
|
||||
.flags(shape.flags);
|
||||
let properties = core.image_format_properties(&info).ok_or_else(gap)?;
|
||||
let mut properties = core.image_format_properties(&info);
|
||||
// A format the device makes without the feedback usage alone is made so, and its draws read
|
||||
// no attachment of their own pass through it.
|
||||
if properties.is_none() && feedback {
|
||||
feedback = false;
|
||||
image_usage &= !vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT;
|
||||
properties = core.image_format_properties(&info.usage(image_usage));
|
||||
}
|
||||
let properties = properties.ok_or_else(gap)?;
|
||||
let max = properties.max_extent;
|
||||
let max_extent = [max.width, max.height, max.depth];
|
||||
if shape.extent.iter().zip(max_extent).any(|(&e, m)| e > m)
|
||||
@@ -1230,6 +1273,7 @@ fn plan(
|
||||
sampled,
|
||||
storage,
|
||||
home,
|
||||
feedback,
|
||||
aspects,
|
||||
unit,
|
||||
word,
|
||||
@@ -1475,6 +1519,7 @@ impl Made {
|
||||
let info = match bind {
|
||||
Bind::InPlace { .. } => image_info(d, shape, plan)
|
||||
.tiling(vk::ImageTiling::LINEAR)
|
||||
.usage(plan.linear_usage())
|
||||
.initial_layout(vk::ImageLayout::PREINITIALIZED),
|
||||
Bind::Pooled | Bind::Heap { .. } => image_info(d, shape, plan),
|
||||
};
|
||||
@@ -2244,6 +2289,7 @@ impl Texture {
|
||||
index,
|
||||
sampled.map(|view| (view, inner.image.home)),
|
||||
storage,
|
||||
inner.image.feedback,
|
||||
)
|
||||
};
|
||||
}
|
||||
@@ -2262,6 +2308,49 @@ impl Texture {
|
||||
self.inner.image.image
|
||||
}
|
||||
|
||||
/// Whether it reaches `level` and `layer` of its image: a view reaches its own levels and
|
||||
/// slices alone.
|
||||
pub(crate) fn reaches(&self, level: u32, layer: u32) -> bool {
|
||||
let d = &self.inner.descriptor;
|
||||
let [first_level, first_layer] = self.inner.base();
|
||||
(first_level..first_level + d.mipmap_level_count).contains(&level)
|
||||
&& (first_layer..first_layer + slices(d)).contains(&layer)
|
||||
}
|
||||
|
||||
/// Whether a draw may read an attachment of its own pass through it, its image in the
|
||||
/// feedback layout ([`Image`]'s `feedback`).
|
||||
pub(crate) fn feedback(&self) -> bool {
|
||||
self.inner.image.feedback
|
||||
}
|
||||
|
||||
/// Its resource id while its image is in the feedback layout: its second element's, at its
|
||||
/// index plus [`SAMPLED_IMAGES`](crate::floor::SAMPLED_IMAGES), with its own word. Nil for a
|
||||
/// texture no shader reaches.
|
||||
pub(crate) fn feedback_resource_id(&self) -> ResourceId {
|
||||
match self.inner.index {
|
||||
Some((_, index)) => ResourceId {
|
||||
index: index + crate::floor::SAMPLED_IMAGES,
|
||||
word: self.inner.word,
|
||||
},
|
||||
None => ResourceId::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Every level and layer of its image, every aspect.
|
||||
pub(crate) fn whole(&self) -> vk::ImageSubresourceRange {
|
||||
let root = match &self.inner.view {
|
||||
Some(view) => &view.parent.inner.descriptor,
|
||||
None => &self.inner.descriptor,
|
||||
};
|
||||
vk::ImageSubresourceRange {
|
||||
aspect_mask: self.inner.image.aspects,
|
||||
base_mip_level: 0,
|
||||
level_count: root.mipmap_level_count,
|
||||
base_array_layer: 0,
|
||||
layer_count: slices(root),
|
||||
}
|
||||
}
|
||||
|
||||
/// Its image's format.
|
||||
pub(crate) fn format(&self) -> vk::Format {
|
||||
self.inner.format
|
||||
|
||||
Reference in new issue
Block a user