rt: a constexpr sampler's index under the owner rule, and an object released with work pending
A constexpr sampler's numbering is a placement like any other. Where a sampler state took the index from it, a later pipeline state that numbers the constexpr sampler there took nothing back: it sampled through the program's sampler. Now it places the constexpr sampler there again with every element it holds (Arrays::hold_samplers: under the grants' lock, the owner's additions where it still owns the index, else a placement), and the constexpr sampler keeps its elements' samplers to write them. The consumer chooses constexpr indices and keeps them from the program's sampler states while a state numbering one lives; rt does not check it (rt README, compute pipeline docs). An object the program released while a command buffer that used it is pending is displaced like a live one: a placement at its index takes it at once, and its teardown once the command buffer retires touches nothing it no longer owns (one owner compare for both). Nothing empties an element at the program's release: a committed command buffer still reads the object through its index, which Metal allows. The owner's teardown nulls it. RESOURCE-ID.md states the duty (no work in flight reads a displaced or released object through its ID) and what an element no object owns holds: every element is written at device creation, a null descriptor (nullDescriptor, on the floor) or the array's default sampler, and written back to that when its owner goes. The replay player gave constexpr samplers the lowest free sampler index, the range the stream's named sampler IDs come from, so a program's sampler state could take a constexpr sampler's index. It now gives them the highest free one, outside that sequence (Indices::highest; REPLAY-STREAM.md). Tests: - sampling::every_device_gives_a_constexpr_sampler_s_index_to_the_object_placed_there_last: a nearest sampler state over sample_up's linear constexpr sampler reads nearest; a later state numbering it takes the index back (linear, for both states); the sampler state's going touches nothing; the slot gauge counts the index once. - sampling::every_device_gives_a_released_object_s_index_to_the_next_at_once: a command buffer samples A through SA, signals and waits on the host; A and SA are released and B and SB made at their indices while it is pending (asserted); another queue reads B through SB; after the first command buffer retires, and A and SA with it, the index still reads B through SB. The writes are valid under UPDATE_UNUSED_WHILE_PENDING with PARTIALLY_BOUND (vkspec: "as long as they are not dynamically used by any shader invocations"): A's dispatch completed before its signal. - replay a_constexpr_sampler_takes_no_index_of_the_sequence.
This commit is contained in:
1 parent
fd916e7f99
commit
fe23decd1b
11 files changed
+444
-124
No files matched your search
@@ -285,14 +285,16 @@ made, with no lock, and never rewritten for layouts.
|
||||
`Device::release_indices` returns a grant; one not granted is `Refusal::IndicesNotGranted`, and
|
||||
one where an object still holds an index `Refusal::IndicesInUse`.
|
||||
- An index has one owner: the object placed there last, in each class. Making an object at an
|
||||
index another live object holds takes the index, as Metal's later texture at a resource ID
|
||||
index another object holds, a live one or one the program released that a command buffer not
|
||||
yet retired still holds, takes the index at once, as Metal's later texture at a resource ID
|
||||
takes the ID: the new object writes its elements (a texture with no storage view writes its
|
||||
storage element null where it took the index from another, so nothing stale answers there),
|
||||
and the earlier object's going touches neither the index nor its elements. No work in flight
|
||||
may read the earlier object through the index: the program's duty, as it is Metal's, which the
|
||||
runtime does not check. Making one at an index not granted is `Refusal::IndicesNotGranted`.
|
||||
The index is free again once its owner's last handle is gone, and with it the last command
|
||||
buffer that used it; its elements read their default again from then.
|
||||
and the earlier object's going, at its release or once its command buffers retire, touches
|
||||
neither the index nor its elements. No work in flight may read a displaced or released object
|
||||
through the index: the program's duty, as it is Metal's, which the runtime does not check.
|
||||
Making one at an index not granted is `Refusal::IndicesNotGranted`. Where nothing took it, the
|
||||
index is free again once its owner's last handle is gone, and with it the last command buffer
|
||||
that used it; its elements read their default again from then.
|
||||
|
||||
## Textures
|
||||
|
||||
@@ -889,9 +891,13 @@ that touch blocks first.
|
||||
coordinate is scaled in the shader) at its index of the three sampler arrays, shared by create
|
||||
info with the sampler states'. States that number one constexpr sampler at one index share its
|
||||
elements; a constexpr sampler of other state words, or a sampler state, made at that index takes
|
||||
it, as any object made at a held index does (above). The last of them to go writes the arrays'
|
||||
defaults back and frees the index, where the constexpr sampler still owns it. The read and write samplers take nothing at their
|
||||
indices. Once `sampler_ids` has answered, a call that fails holds none of the indices.
|
||||
it, as any object made at a held index does (above), and a later state that numbers the
|
||||
constexpr sampler there takes it back, with every element the constexpr sampler holds. The last
|
||||
of its states to go writes the arrays' defaults back and frees the index, where the constexpr
|
||||
sampler still owns it. The consumer keeps a constexpr sampler's index from the program's sampler
|
||||
states while a state numbering it there lives; rt does not check it. The read and write samplers
|
||||
take nothing at their indices. Once `sampler_ids` has answered, a call that fails holds none of
|
||||
the indices.
|
||||
- Every kernel is lowered once at creation. The translator answers, lowering nothing
|
||||
(`Device::kernel_observes_partition`), whether it observes its threadgroup partition (it holds
|
||||
threadgroup memory, a barrier, fence or atomic of threadgroup or SIMD-group scope, a SIMD-group
|
||||
|
||||
@@ -494,6 +494,54 @@ impl Arrays {
|
||||
elements: &[(usize, Option<vk::Sampler>)],
|
||||
) -> Result<Owner, Error> {
|
||||
let mut grants = lock(&self.grants);
|
||||
// SAFETY: the caller's contract, and the lock is held.
|
||||
unsafe { self.place_samplers_locked(&mut grants, device, counters, index, elements) }
|
||||
}
|
||||
|
||||
/// One more pipeline state numbers the constexpr sampler of `owner` at `index`. Where the
|
||||
/// constexpr sampler still owns the index, writes `added`, the elements of the arrays the
|
||||
/// state adds, as [`Arrays::place_samplers`] does, and answers `owner`. Where another object
|
||||
/// took the index since, the state's numbering is a placement like any other: it places the
|
||||
/// constexpr sampler there again with `all`, every element it holds, and answers the new
|
||||
/// owner word.
|
||||
///
|
||||
/// Err: [`Refusal::IndicesNotGranted`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// No pending work uses an element of `added`, each sampler is live and of the device
|
||||
/// whenever work uses it, and no work in flight reads a displaced object through the index.
|
||||
pub(crate) unsafe fn hold_samplers(
|
||||
&self,
|
||||
device: &ash::Device,
|
||||
counters: &Counters,
|
||||
(index, owner): (u32, Owner),
|
||||
added: &[(usize, Option<vk::Sampler>)],
|
||||
all: &[(usize, Option<vk::Sampler>)],
|
||||
) -> Result<Owner, Error> {
|
||||
let mut grants = lock(&self.grants);
|
||||
if grants.owns(IndexClass::Sampler, index, owner) {
|
||||
// SAFETY: the caller's contract; the object owns the index, and the lock is held.
|
||||
unsafe { self.write_samplers(device, index, added) };
|
||||
return Ok(owner);
|
||||
}
|
||||
// SAFETY: the caller's contract, and the lock is held.
|
||||
unsafe { self.place_samplers_locked(&mut grants, device, counters, index, all) }
|
||||
}
|
||||
|
||||
/// [`Arrays::place_samplers`] under the grants' lock, `grants`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// As [`Arrays::place_samplers`]; `grants` is the guard of `self.grants`.
|
||||
unsafe fn place_samplers_locked(
|
||||
&self,
|
||||
grants: &mut Grants,
|
||||
device: &ash::Device,
|
||||
counters: &Counters,
|
||||
index: u32,
|
||||
elements: &[(usize, Option<vk::Sampler>)],
|
||||
) -> Result<Owner, Error> {
|
||||
let Placed { owner, displaced } = grants.place(IndexClass::Sampler, index)?;
|
||||
if !displaced {
|
||||
counters.rise(Gauge::DescriptorSlots);
|
||||
@@ -512,28 +560,6 @@ impl Arrays {
|
||||
Ok(owner)
|
||||
}
|
||||
|
||||
/// Writes `elements` at `index` as [`Arrays::place_samplers`] does, where the object of
|
||||
/// `owner` still owns the index: a later holder of a constexpr sampler adds the arrays it
|
||||
/// uses. Elsewhere it writes nothing.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// No pending work uses an element it writes, and each sampler is live and of the device
|
||||
/// whenever work uses it.
|
||||
pub(crate) unsafe fn add_samplers(
|
||||
&self,
|
||||
device: &ash::Device,
|
||||
index: u32,
|
||||
owner: Owner,
|
||||
elements: &[(usize, Option<vk::Sampler>)],
|
||||
) {
|
||||
let grants = lock(&self.grants);
|
||||
if grants.owns(IndexClass::Sampler, index, owner) {
|
||||
// SAFETY: the caller's contract; the object owns the index, and the lock is held.
|
||||
unsafe { self.write_samplers(device, index, elements) };
|
||||
}
|
||||
}
|
||||
|
||||
/// The sampler state or constexpr sampler of `owner` goes from `index`: where it still owns
|
||||
/// the index, the index is free and each array's element its default again, and the
|
||||
/// descriptor-slot gauge falls. A displaced object's going touches nothing.
|
||||
|
||||
@@ -468,9 +468,13 @@ impl crate::Device {
|
||||
/// (`CombinedSampler::constexpr_sampler`, shared by create info as a sampler state's are) at
|
||||
/// its index of the sampler arrays before the call returns, and every dispatch pushes the
|
||||
/// index; states that number one constexpr sampler at one index share its elements, and the
|
||||
/// last of them to go gives them back. The read and write samplers take nothing at their
|
||||
/// indices. What `sampler_ids` answers as an error, the call answers; once it has answered,
|
||||
/// a failing call holds none of the indices.
|
||||
/// last of them to go gives them back. A constexpr sampler's index has one owner, as a
|
||||
/// texture's has ([`Device::new_texture`]): a sampler state made there takes it, and a later
|
||||
/// state numbering the constexpr sampler there takes it back. The consumer keeps the index
|
||||
/// from the program's sampler states while a state numbering it there lives; the runtime does
|
||||
/// not check it. The read and write samplers take nothing at their indices. What
|
||||
/// `sampler_ids` answers as an error, the call answers; once it has answered, a failing call
|
||||
/// holds none of the indices.
|
||||
///
|
||||
/// Err: [`Error::Translation`], the translator's typed refusal or a descriptor Metal refuses;
|
||||
/// [`Gap::IndirectCommandBuffers`]; [`Gap::KernelInput`] for a kernel that reads what the
|
||||
|
||||
@@ -140,21 +140,24 @@ fn every(samplers: [Option<vk::Sampler>; 3]) -> [(usize, Option<vk::Sampler>); 3
|
||||
}
|
||||
|
||||
/// A constexpr sampler at an index of the sampler arrays, by its index and state words: the
|
||||
/// create info of each array's element it holds there (plain, comparison, integer texture), how
|
||||
/// many pipeline states number it there, and its owner word at the index.
|
||||
/// create info and sampler of each array's element it holds there (plain, comparison, integer
|
||||
/// texture), how many pipeline states number it there, and its owner word at the index.
|
||||
pub(crate) struct Constexpr {
|
||||
uses: [Option<SamplerDesc>; 3],
|
||||
uses: [Option<(SamplerDesc, vk::Sampler)>; 3],
|
||||
holders: u32,
|
||||
owner: Owner,
|
||||
}
|
||||
|
||||
/// One more pipeline state numbers the constexpr sampler of state words `words` at `index`, with
|
||||
/// the create info of each array's element it uses there (`None` for an array it does not use).
|
||||
/// The first holder places it at the index and writes its elements, taking the index from a
|
||||
/// sampler state or a constexpr sampler of other words there, as any placement does
|
||||
/// ([`Arrays::place_samplers`](crate::arrays::Arrays::place_samplers)); a later one of the same
|
||||
/// words writes the elements of the arrays it adds and leaves the others, which pending work may
|
||||
/// read, where the constexpr sampler still owns the index.
|
||||
/// Each numbering is a placement, as any is
|
||||
/// ([`Arrays::place_samplers`](crate::arrays::Arrays::place_samplers)): the first holder places it
|
||||
/// at the index and writes its elements, taking the index from a sampler state or a constexpr
|
||||
/// sampler of other words there. A later one of the same words writes the elements of the arrays
|
||||
/// it adds and leaves the others, which pending work may read, where the constexpr sampler still
|
||||
/// owns the index; where another object took the index since, it places the constexpr sampler
|
||||
/// there again with every element it holds
|
||||
/// ([`Arrays::hold_samplers`](crate::arrays::Arrays::hold_samplers)).
|
||||
///
|
||||
/// Err: [`Refusal::IndicesNotGranted`](crate::Refusal::IndicesNotGranted);
|
||||
/// [`Limit::DeviceSamplers`]; [`Error::Vulkan`].
|
||||
@@ -191,33 +194,49 @@ pub(crate) fn hold_constexpr(
|
||||
.iter()
|
||||
.map(|&(array, _, sampler)| (array, Some(sampler)))
|
||||
.collect();
|
||||
let owner = match present {
|
||||
// SAFETY: no work reads an element it had not written (no state numbering it there uses
|
||||
// that array), and each sampler is held while its element holds it.
|
||||
Some((_, owner)) => {
|
||||
unsafe { (core.arrays).add_samplers(&core.device, index, owner, &elements) };
|
||||
owner
|
||||
}
|
||||
// SAFETY: each sampler is held while its element holds it.
|
||||
None => match unsafe {
|
||||
(core.arrays).place_samplers(&core.device, &core.counters, index, &elements)
|
||||
} {
|
||||
Ok(owner) => owner,
|
||||
Err(error) => {
|
||||
let taken: Vec<Option<SamplerDesc>> =
|
||||
added.iter().map(|&(_, desc, _)| Some(desc)).collect();
|
||||
give_back(core, &taken);
|
||||
return Err(error);
|
||||
let placed = match present {
|
||||
Some((present, owner)) => {
|
||||
let held = present
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(array, used)| used.map(|(_, sampler)| (array, Some(sampler))));
|
||||
let all: Vec<(usize, Option<vk::Sampler>)> = held.chain(elements.clone()).collect();
|
||||
// SAFETY: no work reads an element of `elements` (no state numbering it there uses
|
||||
// that array), each sampler is held while its element holds it, and no work in
|
||||
// flight reads the object it takes the index from (the consumer's duty).
|
||||
unsafe {
|
||||
(core.arrays).hold_samplers(
|
||||
&core.device,
|
||||
&core.counters,
|
||||
(index, owner),
|
||||
&elements,
|
||||
&all,
|
||||
)
|
||||
}
|
||||
}
|
||||
// SAFETY: each sampler is held while its element holds it, and no work in flight reads
|
||||
// the object it takes the index from (the consumer's duty).
|
||||
None => unsafe {
|
||||
(core.arrays).place_samplers(&core.device, &core.counters, index, &elements)
|
||||
},
|
||||
};
|
||||
let owner = match placed {
|
||||
Ok(owner) => owner,
|
||||
Err(error) => {
|
||||
let taken: Vec<Option<SamplerDesc>> =
|
||||
added.iter().map(|&(_, desc, _)| Some(desc)).collect();
|
||||
give_back(core, &taken);
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
let entry = held.entry((index, words)).or_insert(Constexpr {
|
||||
uses: [None; 3],
|
||||
holders: 0,
|
||||
owner,
|
||||
});
|
||||
for (array, desc, _) in added {
|
||||
entry.uses[array] = Some(desc);
|
||||
entry.owner = owner;
|
||||
for (array, desc, sampler) in added {
|
||||
entry.uses[array] = Some((desc, sampler));
|
||||
}
|
||||
entry.holders += 1;
|
||||
Ok(())
|
||||
@@ -242,7 +261,7 @@ pub(crate) fn release_constexpr(core: &Core, index: u32, words: [u64; 2]) {
|
||||
// numbering it held the state until it retired.
|
||||
unsafe { (core.arrays).vacate_samplers(&core.device, &core.counters, index, entry.owner) };
|
||||
drop(held);
|
||||
give_back(core, &entry.uses);
|
||||
give_back(core, &entry.uses.map(|used| used.map(|(desc, _)| desc)));
|
||||
}
|
||||
|
||||
/// Gives up one hold of each sampler of `uses`, the reaper freeing each one no element holds.
|
||||
@@ -266,7 +285,8 @@ impl Device {
|
||||
/// device's samplers, which share one `VkSampler` per create info, and written into the three
|
||||
/// sampler arrays at `index` before the call returns. The comparison array takes the state's
|
||||
/// sampler where its compare function is not `Never`, and keeps its default otherwise. An
|
||||
/// index another live state or constexpr sampler holds is taken, as a texture's is
|
||||
/// index another state or a constexpr sampler holds, live or released and held by a command
|
||||
/// buffer not yet retired, is taken at once, as a texture's is
|
||||
/// ([`Device::new_texture`](crate::Device::new_texture)): the earlier one's going leaves it.
|
||||
///
|
||||
/// Err: [`Refusal::IndicesNotGranted`](crate::Refusal::IndicesNotGranted);
|
||||
|
||||
@@ -8,19 +8,22 @@
|
||||
//! it, in one dispatch. An index belongs to the texture placed there last: sampling and writing
|
||||
//! through it reach that texture, a displaced texture's going touches nothing, a placement of a
|
||||
//! texture with no storage view empties the storage element an earlier one wrote, and once the
|
||||
//! last placed goes, the index samples zero.
|
||||
//! last placed goes, the index samples zero. The rule holds at a constexpr sampler's index, and
|
||||
//! over a texture and a sampler state the program released while a command buffer that used
|
||||
//! them is still pending.
|
||||
//!
|
||||
//! `llvm-as` assembles the kernels; without it the test skips visibly.
|
||||
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod on_device {
|
||||
use crate::device::lock;
|
||||
use crate::tests::command::on_device::{each_device, finished};
|
||||
use crate::tests::command::on_device::{each_device, finished, shared_event, PATIENCE};
|
||||
use crate::{
|
||||
CommandQueue, ComputePipelineDescriptor, ComputePipelineState, Device, DispatchType, Error,
|
||||
FunctionDescriptor, IndexClass, Library, Limit, NumberedSampler, PixelFormat, Refusal,
|
||||
Region, SamplerDescriptor, SamplerMinMagFilter, SamplerState, StageSource, Texture,
|
||||
TextureDescriptor, TextureType, TextureUsage,
|
||||
CommandBuffer, CommandBufferStatus, CommandQueue, ComputePipelineDescriptor,
|
||||
ComputePipelineState, Device, DispatchType, Error, FunctionDescriptor, IndexClass, Library,
|
||||
Limit, NumberedSampler, PixelFormat, Refusal, Region, SamplerDescriptor,
|
||||
SamplerMinMagFilter, SamplerState, StageSource, Texture, TextureDescriptor, TextureType,
|
||||
TextureUsage,
|
||||
};
|
||||
use metal2vulkan_devtools::{fixtures_dir, llvm_assemble, AssembleError};
|
||||
|
||||
@@ -125,6 +128,11 @@ mod on_device {
|
||||
|
||||
/// Writes every texel of `source`, `SIDE` a side, [`texel`].
|
||||
fn fill(name: &str, source: &Texture) {
|
||||
fill_with(name, source, texel);
|
||||
}
|
||||
|
||||
/// Writes every texel `(x, y)` of `source`, `SIDE` a side, `texel(x, y)`.
|
||||
fn fill_with(name: &str, source: &Texture, texel: fn(u32, u32) -> [f32; 4]) {
|
||||
let bytes: Vec<u8> = (0..SIDE)
|
||||
.flat_map(|y| (0..SIDE).flat_map(move |x| texel(x, y)))
|
||||
.flat_map(|v| half(v).to_le_bytes())
|
||||
@@ -173,10 +181,24 @@ mod on_device {
|
||||
fn sample_down(
|
||||
name: &str,
|
||||
queue: &CommandQueue,
|
||||
pass: (&ComputePipelineState, &SamplerState),
|
||||
textures: [&Texture; 2],
|
||||
) {
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
encode_down(name, &command_buffer, pass, textures);
|
||||
command_buffer.commit().expect("a commit");
|
||||
finished(name, &command_buffer);
|
||||
assert!(command_buffer.error().is_none(), "{name}");
|
||||
}
|
||||
|
||||
/// Encodes one dispatch of `sample_down` (`down`) into `command_buffer`, sampling `source`,
|
||||
/// `SIDE` a side, through `sampler` into `target`'s first half of each side.
|
||||
fn encode_down(
|
||||
name: &str,
|
||||
command_buffer: &CommandBuffer,
|
||||
(down, sampler): (&ComputePipelineState, &SamplerState),
|
||||
[source, target]: [&Texture; 2],
|
||||
) {
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
let mut encoder = command_buffer
|
||||
.compute_command_encoder(DispatchType::Serial)
|
||||
.expect("an encoder");
|
||||
@@ -190,9 +212,72 @@ mod on_device {
|
||||
.dispatch_threads([SIDE / 2, SIDE / 2, 1], [4, 4, 1])
|
||||
.unwrap_or_else(|error| panic!("{name}: sample_down: {error}"));
|
||||
encoder.end_encoding();
|
||||
}
|
||||
|
||||
/// Whether a state's numbered samplers are `sample_up`'s: its constexpr sampler, then its
|
||||
/// texture write's.
|
||||
fn up_samplers(s: &[NumberedSampler]) -> bool {
|
||||
matches!(s, [NumberedSampler::Constexpr(_), NumberedSampler::Write])
|
||||
}
|
||||
|
||||
/// A state of the harness's `sample_up`, its constexpr sampler at `CONSTEXPR`.
|
||||
fn up_state(name: &str, device: &Device, library: &Library) -> ComputePipelineState {
|
||||
state(
|
||||
device,
|
||||
library,
|
||||
"sample_up",
|
||||
up_samplers,
|
||||
vec![CONSTEXPR, 0],
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("{name}: sample_up: {error}"))
|
||||
}
|
||||
|
||||
/// One dispatch of `sample_up` (`up`) in a command buffer of its own, sampling `middle`, half
|
||||
/// `SIDE` a side, through its constexpr sampler into `output`, `SIDE` a side, committed and
|
||||
/// waited for: `output`'s texels.
|
||||
fn sample_up(
|
||||
name: &str,
|
||||
queue: &CommandQueue,
|
||||
up: &ComputePipelineState,
|
||||
[middle, output]: [&Texture; 2],
|
||||
) -> Vec<f32> {
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
let mut encoder = command_buffer
|
||||
.compute_command_encoder(DispatchType::Serial)
|
||||
.expect("an encoder");
|
||||
encoder.set_compute_pipeline_state(up).expect("up");
|
||||
encoder.set_texture(Some(middle), 0).expect("middle");
|
||||
encoder.set_texture(Some(output), 1).expect("output");
|
||||
encoder
|
||||
.dispatch_threads([SIDE, SIDE, 1], [4, 4, 1])
|
||||
.unwrap_or_else(|error| panic!("{name}: sample_up: {error}"));
|
||||
encoder.end_encoding();
|
||||
command_buffer.commit().expect("a commit");
|
||||
finished(name, &command_buffer);
|
||||
assert!(command_buffer.error().is_none(), "{name}");
|
||||
texels(output, SIDE)
|
||||
}
|
||||
|
||||
/// What `sample_up` writes of `mean`, half `SIDE` a side, through a linear filter: each texel
|
||||
/// a quarter of a middle texel from a middle texel's center, clamped to the edge.
|
||||
fn upsampled(mean: &[f32]) -> Vec<f32> {
|
||||
let half_side = SIDE / 2;
|
||||
let taps = |x: u32| {
|
||||
let k = x / 2;
|
||||
match x % 2 {
|
||||
0 => (k.saturating_sub(1), k, 0.75f32),
|
||||
_ => (k, (k + 1).min(half_side - 1), 0.25f32),
|
||||
}
|
||||
};
|
||||
let mid = |x: u32, y: u32, c: u32| mean[(4 * (y * half_side + x) + c) as usize];
|
||||
(0..SIDE)
|
||||
.flat_map(|y| (0..SIDE).flat_map(move |x| (0..4).map(move |c| (x, y, c))))
|
||||
.map(|(x, y, c)| {
|
||||
let ((x0, x1, wx), (y0, y1, wy)) = (taps(x), taps(y));
|
||||
let row = |y| mid(x0, y, c) * (1.0 - wx) + mid(x1, y, c) * wx;
|
||||
row(y0) * (1.0 - wy) + row(y1) * wy
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Whether `got` is the linear filter's `want`, within what a device's filter weights round.
|
||||
@@ -309,24 +394,7 @@ mod on_device {
|
||||
let mean = means();
|
||||
let got = texels(&middle, half_side);
|
||||
assert!(near(&got, &mean), "{name}: sample_down {got:?} {mean:?}");
|
||||
// Up: each texel a quarter of a middle texel from a middle texel's center, clamped to
|
||||
// the edge.
|
||||
let taps = |x: u32| {
|
||||
let k = x / 2;
|
||||
match x % 2 {
|
||||
0 => (k.saturating_sub(1), k, 0.75f32),
|
||||
_ => (k, (k + 1).min(half_side - 1), 0.25f32),
|
||||
}
|
||||
};
|
||||
let mid = |x: u32, y: u32, c: u32| mean[(4 * (y * half_side + x) + c) as usize];
|
||||
let upsampled: Vec<f32> = (0..SIDE)
|
||||
.flat_map(|y| (0..SIDE).flat_map(move |x| (0..4).map(move |c| (x, y, c))))
|
||||
.map(|(x, y, c)| {
|
||||
let ((x0, x1, wx), (y0, y1, wy)) = (taps(x), taps(y));
|
||||
let row = |y| mid(x0, y, c) * (1.0 - wx) + mid(x1, y, c) * wx;
|
||||
row(y0) * (1.0 - wy) + row(y1) * wy
|
||||
})
|
||||
.collect();
|
||||
let upsampled = upsampled(&mean);
|
||||
let got = texels(&output, SIDE);
|
||||
assert!(
|
||||
near(&got, &upsampled),
|
||||
@@ -477,6 +545,165 @@ mod on_device {
|
||||
});
|
||||
}
|
||||
|
||||
/// The owner rule at a constexpr sampler's index: a sampler state of nearest filter made at
|
||||
/// the index `sample_up`'s constexpr sampler (linear) holds takes it, and `sample_up` samples
|
||||
/// through the state's element; a later state that numbers the constexpr sampler there takes
|
||||
/// the index back, for every state that numbers it, and the sampler state's going touches
|
||||
/// nothing. The descriptor-slot gauge counts the index once throughout, and not after the
|
||||
/// last state that numbers the constexpr sampler.
|
||||
#[test]
|
||||
fn every_device_gives_a_constexpr_sampler_s_index_to_the_object_placed_there_last() {
|
||||
let Some(library) = library() else {
|
||||
return;
|
||||
};
|
||||
each_device("sampling", |name, _, device| {
|
||||
let counters = device.counters();
|
||||
let grants = [IndexClass::Texture, IndexClass::Sampler].map(|class| {
|
||||
(device.grant_indices(class, 1, 4))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"))
|
||||
});
|
||||
let read = TextureUsage::SHADER_READ;
|
||||
let write = read | TextureUsage::SHADER_WRITE;
|
||||
let source = texture(&device, SIDE, read, 1);
|
||||
let middle = texture(&device, SIDE / 2, write, 2);
|
||||
let output = texture(&device, SIDE, write, 3);
|
||||
fill(name, &source);
|
||||
let sampler = linear(name, &device, 1);
|
||||
let down = down_state(name, &device, &library);
|
||||
let queue = device.new_command_queue(4).expect("a queue");
|
||||
sample_down(name, &queue, (&down, &sampler), [&source, &middle]);
|
||||
let mean = means();
|
||||
let linear_up = upsampled(&mean);
|
||||
// Nearest: each texel the middle texel it lies in.
|
||||
let half_side = SIDE / 2;
|
||||
let nearest_up: Vec<f32> = (0..SIDE)
|
||||
.flat_map(|y| (0..SIDE).flat_map(move |x| (0..4).map(move |c| (x, y, c))))
|
||||
.map(|(x, y, c)| mean[(4 * ((y / 2) * half_side + x / 2) + c) as usize])
|
||||
.collect();
|
||||
let up_pass =
|
||||
|up: &ComputePipelineState| sample_up(name, &queue, up, [&middle, &output]);
|
||||
|
||||
let slots = counters.gauges().descriptor_slots;
|
||||
let up = up_state(name, &device, &library);
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots + 1, "{name}");
|
||||
let got = up_pass(&up);
|
||||
assert!(
|
||||
near(&got, &linear_up),
|
||||
"{name}: the constexpr sampler {got:?}"
|
||||
);
|
||||
|
||||
let nearest = (device.new_sampler_state(&SamplerDescriptor::new(), CONSTEXPR))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots + 1, "{name}");
|
||||
let got = up_pass(&up);
|
||||
assert!(
|
||||
near(&got, &nearest_up),
|
||||
"{name}: the sampler state made over it {got:?}"
|
||||
);
|
||||
|
||||
let again = up_state(name, &device, &library);
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots + 1, "{name}");
|
||||
let got = up_pass(&up);
|
||||
assert!(near(&got, &linear_up), "{name}: taken back {got:?}");
|
||||
drop(nearest);
|
||||
let got = up_pass(&again);
|
||||
assert!(
|
||||
near(&got, &linear_up),
|
||||
"{name}: once the sampler state went {got:?}"
|
||||
);
|
||||
drop((up, again));
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots, "{name}");
|
||||
drop((queue, down, sampler, source, middle, output));
|
||||
for grant in grants {
|
||||
(device.release_indices(grant)).unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The owner rule over objects the program released while work that used them is pending: a
|
||||
/// command buffer samples texture A through sampler state SA, signals once that is done, and
|
||||
/// waits on the host. A and SA are released, and texture B and sampler state SB are made at
|
||||
/// their indices at once, with the command buffer still pending; sampling through the indices
|
||||
/// on another queue reads B through SB. Once the command buffer has retired, and A and SA with
|
||||
/// it, sampling through the indices still reads B through SB. Nothing reads A or SA through
|
||||
/// the indices once B and SB take them: A's dispatch had completed (its signal), so the
|
||||
/// writes are valid under `UPDATE_UNUSED_WHILE_PENDING`. The descriptor-slot gauge counts each
|
||||
/// index once throughout.
|
||||
#[test]
|
||||
fn every_device_gives_a_released_object_s_index_to_the_next_at_once() {
|
||||
const AT: u32 = 3;
|
||||
let Some(library) = library() else {
|
||||
return;
|
||||
};
|
||||
each_device("sampling", |name, _, device| {
|
||||
let counters = device.counters();
|
||||
let grants =
|
||||
[(IndexClass::Texture, 8), (IndexClass::Sampler, 1)].map(|(class, count)| {
|
||||
(device.grant_indices(class, 1, count))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"))
|
||||
});
|
||||
let read = TextureUsage::SHADER_READ;
|
||||
let write = read | TextureUsage::SHADER_WRITE;
|
||||
fn flat(_: u32, _: u32) -> [f32; 4] {
|
||||
[0.5, 0.25, 0.75, 1.0]
|
||||
}
|
||||
let a = texture(&device, SIDE, read, AT);
|
||||
fill_with(name, &a, flat);
|
||||
let [first, second] = [5, 6].map(|index| texture(&device, SIDE / 2, write, index));
|
||||
let sa = linear(name, &device, 1);
|
||||
let down = down_state(name, &device, &library);
|
||||
let [queue, other] = [(); 2].map(|()| device.new_command_queue(4).expect("a queue"));
|
||||
let (signaled, release) = (shared_event(&device), shared_event(&device));
|
||||
let slots = counters.gauges().descriptor_slots;
|
||||
|
||||
let pending = queue.command_buffer().expect("a command buffer");
|
||||
encode_down(name, &pending, (&down, &sa), [&a, &first]);
|
||||
(pending.encode_signal_event(signaled.as_event(), 1)).expect("a signal");
|
||||
(pending.encode_wait_for_event(release.as_event(), 1)).expect("a wait");
|
||||
pending.commit().expect("a commit");
|
||||
assert!(signaled.wait_until_signaled_value(1, PATIENCE), "{name}");
|
||||
drop((a, sa));
|
||||
let b = texture(&device, SIDE, read, AT);
|
||||
fill(name, &b);
|
||||
let sb = linear(name, &device, 1);
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots, "{name}");
|
||||
sample_down(name, &other, (&down, &sb), [&b, &second]);
|
||||
let status = pending.status();
|
||||
assert!(
|
||||
!matches!(
|
||||
status,
|
||||
CommandBufferStatus::Completed | CommandBufferStatus::Error
|
||||
),
|
||||
"{name}: A's command buffer is pending: {status:?}"
|
||||
);
|
||||
let got = texels(&second, SIDE / 2);
|
||||
assert!(
|
||||
near(&got, &means()),
|
||||
"{name}: while A's work is pending {got:?}"
|
||||
);
|
||||
|
||||
release.set_signaled_value(1);
|
||||
finished(name, &pending);
|
||||
assert!(pending.error().is_none(), "{name}");
|
||||
let want: Vec<f32> = (0..SIDE * SIDE / 4).flat_map(|_| flat(0, 0)).collect();
|
||||
let got = texels(&first, SIDE / 2);
|
||||
assert!(near(&got, &want), "{name}: A sampled {got:?}");
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots, "{name}");
|
||||
sample_down(name, &queue, (&down, &sb), [&b, &first]);
|
||||
let got = texels(&first, SIDE / 2);
|
||||
assert!(
|
||||
near(&got, &means()),
|
||||
"{name}: once A's work retired {got:?}"
|
||||
);
|
||||
drop((b, sb));
|
||||
assert_eq!(counters.gauges().descriptor_slots, slots - 2, "{name}");
|
||||
drop((pending, queue, other, down, first, second));
|
||||
for grant in grants {
|
||||
(device.release_indices(grant)).unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// A constexpr sampler past the device's samplers is refused, and the refusal lets go of what
|
||||
/// the state took: the same state is made once the samplers have room.
|
||||
#[test]
|
||||
|
||||
@@ -445,10 +445,12 @@ impl Device {
|
||||
/// after it. Until the next submission nothing else holds the texture: released first, it is
|
||||
/// freed at once.
|
||||
///
|
||||
/// An index another live texture holds is taken, as Metal's later texture at a resource ID
|
||||
/// takes the ID: the new texture owns the index and its elements from then, and the earlier
|
||||
/// one's going touches neither. No work in flight may read the earlier texture through the
|
||||
/// index (the program's duty, as it is Metal's; nothing checks it).
|
||||
/// An index another texture holds, a live one or one the program released that a command
|
||||
/// buffer not yet retired still holds, is taken at once, as Metal's later texture at a
|
||||
/// resource ID takes the ID: the new texture owns the index and its elements from then, and
|
||||
/// the earlier one's going, at its release or once its command buffers retire, touches
|
||||
/// neither. No work in flight may read a displaced or released texture through the index
|
||||
/// (the program's duty, as it is Metal's; nothing checks it).
|
||||
///
|
||||
/// A swizzle composes with the view's own component mapping. Shader-write usage writes the
|
||||
/// storage element where the device stores the format through a storage image without a
|
||||
|
||||
@@ -66,17 +66,25 @@ impl Indices {
|
||||
}
|
||||
}
|
||||
|
||||
/// The lowest index of `class` from `from` that no object holds, the program did not reserve
|
||||
/// and `skip` does not name.
|
||||
pub(in crate::replay) fn lowest(
|
||||
&self,
|
||||
class: rt::IndexClass,
|
||||
from: u32,
|
||||
skip: impl Fn(u32) -> bool,
|
||||
) -> Option<u32> {
|
||||
/// The lowest index of `class` from `from` that no object holds and the program did not
|
||||
/// reserve.
|
||||
pub(in crate::replay) fn lowest(&self, class: rt::IndexClass, from: u32) -> Option<u32> {
|
||||
let (held, reserved) = (&self.held[slot(class)], &self.reserved[slot(class)]);
|
||||
let range = class.range();
|
||||
(from.max(range.start)..range.end)
|
||||
.find(|index| !held.contains_key(index) && !reserved.contains(index))
|
||||
}
|
||||
|
||||
/// The highest index of `class` that no object holds, the program did not reserve and `skip`
|
||||
/// does not name: away from the lowest indices, which the program's own objects take.
|
||||
pub(in crate::replay) fn highest(
|
||||
&self,
|
||||
class: rt::IndexClass,
|
||||
skip: impl Fn(u32) -> bool,
|
||||
) -> Option<u32> {
|
||||
let (held, reserved) = (&self.held[slot(class)], &self.reserved[slot(class)]);
|
||||
(class.range())
|
||||
.rev()
|
||||
.find(|index| !held.contains_key(index) && !reserved.contains(index) && !skip(*index))
|
||||
}
|
||||
|
||||
@@ -102,7 +110,7 @@ impl Indices {
|
||||
}
|
||||
}
|
||||
}
|
||||
let index = self.lowest(class, 0, |_| false)?;
|
||||
let index = self.lowest(class, 0)?;
|
||||
let made = make(index);
|
||||
if made.is_ok() {
|
||||
self.hold(Some(object), class, index);
|
||||
@@ -110,20 +118,15 @@ impl Indices {
|
||||
Some(made)
|
||||
}
|
||||
|
||||
/// `make` at the highest index of `class` that no object holds and the program did not
|
||||
/// reserve: one away from the lowest indices, which the program's own objects take; what
|
||||
/// `object` then holds. None where every index of the class is held.
|
||||
/// `make` at the [`highest`](Self::highest) free index of `class`; what `object` then holds.
|
||||
/// None where every index of the class is held.
|
||||
pub(in crate::replay) fn top<T>(
|
||||
&mut self,
|
||||
object: u64,
|
||||
class: rt::IndexClass,
|
||||
make: impl Fn(u32) -> Result<T, rt::Error>,
|
||||
) -> Option<Result<T, rt::Error>> {
|
||||
let range = class.range();
|
||||
let (held, reserved) = (&self.held[slot(class)], &self.reserved[slot(class)]);
|
||||
let index = (range.clone())
|
||||
.rev()
|
||||
.find(|index| !held.contains_key(index) && !reserved.contains(index))?;
|
||||
let index = self.highest(class, |_| false)?;
|
||||
let made = make(index);
|
||||
if made.is_ok() {
|
||||
self.hold(Some(object), class, index);
|
||||
|
||||
@@ -922,7 +922,9 @@ impl NewRuntime {
|
||||
}
|
||||
|
||||
/// The pipeline state `make` makes on the device with `ids`, which gives each numbered
|
||||
/// constexpr sampler an index of its own, the lowest free one, the same words one index.
|
||||
/// constexpr sampler an index of its own, the highest free one, the same words one index: the
|
||||
/// stream names no index for it, and the program's sampler states take the lowest, which a
|
||||
/// constexpr sampler there would lose to them.
|
||||
fn numbered<T>(
|
||||
&mut self,
|
||||
make: impl FnOnce(
|
||||
@@ -944,10 +946,10 @@ impl NewRuntime {
|
||||
.or_else(|| taken.iter().find(|(w, _)| w == words).map(|&(_, i)| i));
|
||||
let index = match known {
|
||||
Some(index) => index,
|
||||
// The lowest free sampler index this call has not taken yet.
|
||||
// The highest free sampler index this call has not taken yet.
|
||||
None => {
|
||||
let fresh = |index| taken.iter().any(|&(_, i)| i == index);
|
||||
let index = indices.lowest(rt::IndexClass::Sampler, 0, fresh);
|
||||
let index = indices.highest(rt::IndexClass::Sampler, fresh);
|
||||
// None free: the next would be past the class's range.
|
||||
let index = index.ok_or(rt::Error::Refused(
|
||||
rt::Refusal::IndicesNotGranted {
|
||||
|
||||
@@ -1151,6 +1151,28 @@ fn a_texture_marked_outside_takes_no_index_of_the_sequence() {
|
||||
assert_eq!((marked, marked_again), (end - 1, end - 2));
|
||||
}
|
||||
|
||||
/// The new runtime's backend gives a constexpr sampler, which the stream names no index for, the
|
||||
/// highest free sampler index, past those the same call took, and leaves the sequence the
|
||||
/// program's sampler states take where it was: a sampler state at its index would take it.
|
||||
#[test]
|
||||
fn a_constexpr_sampler_takes_no_index_of_the_sequence() {
|
||||
let class = rt::IndexClass::Sampler;
|
||||
let (start, end) = (class.range().start, class.range().end);
|
||||
let make = |index: u32| Ok::<u32, rt::Error>(index);
|
||||
let mut indices = Indices::default();
|
||||
let first = indices.place(1, class, None, make);
|
||||
assert_eq!(first.expect("a free index").expect("made"), start);
|
||||
let constexpr = indices.highest(class, |_| false);
|
||||
assert_eq!(constexpr, Some(end - 1));
|
||||
indices.hold(None, class, end - 1);
|
||||
assert_eq!(
|
||||
indices.highest(class, |index| index == end - 2),
|
||||
Some(end - 3)
|
||||
);
|
||||
let second = indices.place(2, class, None, make);
|
||||
assert_eq!(second.expect("a free index").expect("made"), start + 1);
|
||||
}
|
||||
|
||||
/// An object takes the index its record names; else, and for an object with none, the lowest
|
||||
/// index of its class that no live object holds and the program did not reserve. A named index an
|
||||
/// object holds is taken from it, and the earlier object's release leaves it; a released object's
|
||||
@@ -1178,7 +1200,7 @@ fn objects_take_the_named_index_else_the_lowest_free_one() {
|
||||
// A named index an object holds: taken, and the earlier object's release leaves it.
|
||||
assert_eq!(take(&mut indices, 5, Some(start + 7)), start + 7);
|
||||
indices.give(1);
|
||||
assert_eq!(indices.lowest(class, start + 7, |_| false), Some(start + 8));
|
||||
assert_eq!(indices.lowest(class, start + 7), Some(start + 8));
|
||||
assert_eq!(
|
||||
take(&mut indices, 6, Some(start + 2)),
|
||||
start + 2,
|
||||
@@ -1191,7 +1213,7 @@ fn objects_take_the_named_index_else_the_lowest_free_one() {
|
||||
"free again after the release"
|
||||
);
|
||||
indices.give(5);
|
||||
assert_eq!(indices.lowest(class, start + 7, |_| false), Some(start + 7));
|
||||
assert_eq!(indices.lowest(class, start + 7), Some(start + 7));
|
||||
assert!(
|
||||
indices.line().starts_with("indices named 3:"),
|
||||
"{}",
|
||||
|
||||
@@ -183,7 +183,9 @@ and the (name id, value) answer pairs.
|
||||
only where the stream names it. A named index a live object of the play holds is taken from
|
||||
it, as Metal's later texture at an ID takes the ID: the earlier object's release leaves the
|
||||
index. Where the runtime refuses a named index (it lies outside the object's class), the object
|
||||
takes the lowest free one, and the report counts it.
|
||||
takes the lowest free one, and the report counts it. A constexpr sampler, which the stream names
|
||||
no index for, takes the highest free sampler index on the new runtime, outside the sequence the
|
||||
program's sampler states take, so none of theirs takes its index.
|
||||
- **Textures outside the program's IDs.** A texture the recorder makes for itself, not the
|
||||
program's (the host's cursor), comes from a texture descriptor record whose map holds
|
||||
`outside: true` and a device's `newTextureWithDescriptor:` of that descriptor. Live, it took no
|
||||
|
||||
+15
-9
@@ -30,12 +30,13 @@ shader reaches has no index. A texture view takes its own index of the texture c
|
||||
buffer has no index: it is a typed refusal under these domains.
|
||||
|
||||
An index has one owner, the object placed there last, as Metal's later texture at a resource ID
|
||||
takes the ID. A texture or a sampler placed at an index another live object holds writes its
|
||||
elements and owns the index; where the earlier texture had a storage element and the later has
|
||||
none, the later one writes that element null, so nothing stale answers there. The earlier
|
||||
object's release touches neither the index nor its elements; the owner's release empties them.
|
||||
The rule rests on the duty Metal puts on a program: no work in flight reads the displaced object
|
||||
through that ID. The runtime does not check it.
|
||||
takes the ID. A texture or a sampler placed at an index another object holds, a live one or one
|
||||
the program released that work in flight still holds, writes its elements and owns the index at
|
||||
once; where the earlier texture had a storage element and the later has none, the later one
|
||||
writes that element null, so nothing stale answers there. The earlier object's release, and its
|
||||
teardown once its work retires, touch neither the index nor its elements; the owner's empties
|
||||
them. The rule rests on the duty Metal puts on a program: no work in flight reads a displaced or
|
||||
released object through that ID. The runtime does not check it.
|
||||
|
||||
## The arrays
|
||||
|
||||
@@ -44,9 +45,14 @@ array of 65,536 elements, binding 1 the storage-image array of 65,536, and bindi
|
||||
sampler arrays of 4,000 elements each, every one partially bound, updated after binding and while
|
||||
unused elements are pending, and seen by every stage. Element `i` of each image array is the
|
||||
texture whose ID holds index `i`, so the consumer writes an element once, as it makes the texture,
|
||||
for every pipeline. Element 0 is a null descriptor (`nullDescriptor`): a nil
|
||||
texture sizes 0, reads and samples zero, and drops a store, as Metal answers for a nil texture, with
|
||||
no code in the shader. Each texture class a shader declares (its shape, numeric type and depth
|
||||
for every pipeline. Element 0 is a null descriptor (`nullDescriptor`, which the floor requires): a
|
||||
nil texture sizes 0, reads and samples zero, and drops a store, as Metal answers for a nil texture,
|
||||
with no code in the shader. So is every image element no texture owns: the runtime writes every
|
||||
element at device creation, a null descriptor in each image array and a default sampler (nearest,
|
||||
clamped to the edge; the comparison array's compares with `NEVER`) in each sampler array, as
|
||||
Vulkan has no null sampler, and writes an element back to that once its owner goes. An index
|
||||
nobody owns, nil, freed or stale, reads as nil and cannot reach a destroyed view or sampler. Each
|
||||
texture class a shader declares (its shape, numeric type and depth
|
||||
comparison, sampled, or storage without a format) is a variable of its descriptor type's binding.
|
||||
|
||||
A shader takes a texture slot's ID from the slot's push field (`PushField::SlotGuestBase`), and an
|
||||
|
||||
Reference in new issue
Block a user