use crate::{DrawIndirect, DrawIndirectCount, HalError};
pub const INDIRECT_OFFSET_ALIGNMENT: u64 = 4;
pub const DRAW_ARGS_BYTES: u64 = 16;
pub const DRAW_INDEXED_ARGS_BYTES: u64 = 20;
pub const MESH_DISPATCH_ARGS_BYTES: u64 = 12;
pub const DRAW_COUNT_BYTES: u64 = 4;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndirectPlan {
pub first: u64,
pub stride: u64,
pub count: u32,
}
impl IndirectPlan {
pub const fn offset(self, index: u32) -> u64 {
self.first + self.stride * index as u64
}
}
pub fn plan_mesh_indirect(
draw: &DrawIndirect,
length: u64,
) -> Result<Option<IndirectPlan>, HalError> {
plan_structures(draw, MESH_DISPATCH_ARGS_BYTES, length)
}
pub fn check_layout(draw: &DrawIndirect, args: u64) -> Result<(), HalError> {
if draw.draw_count == 0 {
return Ok(());
}
if !draw.offset.is_multiple_of(INDIRECT_OFFSET_ALIGNMENT) {
return Err(HalError::InvalidDescriptor(format!(
"an indirect draw's argument offset {} is not a multiple of {INDIRECT_OFFSET_ALIGNMENT}",
draw.offset
)));
}
if draw.draw_count > 1 {
let stride = u64::from(draw.stride);
if stride < args || !stride.is_multiple_of(INDIRECT_OFFSET_ALIGNMENT) {
return Err(HalError::InvalidDescriptor(format!(
"an indirect draw of {} structures has a stride of {stride}, and one argument \
structure is {args} bytes on a {INDIRECT_OFFSET_ALIGNMENT}-byte alignment",
draw.draw_count
)));
}
}
Ok(())
}
pub fn plan_structures(
draw: &DrawIndirect,
args: u64,
length: u64,
) -> Result<Option<IndirectPlan>, HalError> {
if draw.draw_count == 0 {
return Ok(None);
}
check_layout(draw, args)?;
let stride = if draw.draw_count == 1 {
args
} else {
u64::from(draw.stride)
};
let span = u64::from(draw.draw_count - 1)
.checked_mul(stride)
.and_then(|span| span.checked_add(args))
.and_then(|span| draw.offset.checked_add(span));
if span.is_none_or(|span| span > length) {
return Err(HalError::InvalidDescriptor(format!(
"an indirect draw of {} structures {stride} bytes apart from offset {} runs past a \
{length}-byte buffer",
draw.draw_count, draw.offset
)));
}
Ok(Some(IndirectPlan {
first: draw.offset,
stride,
count: draw.draw_count,
}))
}
pub fn plan_count_structures(
draw: &DrawIndirectCount,
args: u64,
args_length: u64,
count_length: u64,
) -> Result<Option<IndirectPlan>, HalError> {
if draw.max_draw_count == 0 {
return Ok(None);
}
if !draw.count_offset.is_multiple_of(INDIRECT_OFFSET_ALIGNMENT) {
return Err(HalError::InvalidDescriptor(format!(
"a count-limited draw's count offset {} is not a multiple of \
{INDIRECT_OFFSET_ALIGNMENT}",
draw.count_offset
)));
}
if draw
.count_offset
.checked_add(DRAW_COUNT_BYTES)
.is_none_or(|end| end > count_length)
{
return Err(HalError::InvalidDescriptor(format!(
"a count-limited draw reads its u32 count at offset {} of a {count_length}-byte \
buffer",
draw.count_offset
)));
}
plan_structures(
&DrawIndirect {
args: draw.args,
offset: draw.args_offset,
draw_count: draw.max_draw_count,
stride: draw.stride,
},
args,
args_length,
)
}
pub const fn structure_bytes(indexed: bool) -> u64 {
if indexed {
DRAW_INDEXED_ARGS_BYTES
} else {
DRAW_ARGS_BYTES
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::BufferHandle;
use crcbl_core::Handle;
fn buffer() -> BufferHandle {
Handle::from_bits(1 << 32).expect("generation 1 is non-zero")
}
fn indirect(offset: u64, draw_count: u32, stride: u32) -> DrawIndirect {
DrawIndirect {
args: buffer(),
offset,
draw_count,
stride,
}
}
fn counted(
args_offset: u64,
count_offset: u64,
max_draw_count: u32,
stride: u32,
) -> DrawIndirectCount {
DrawIndirectCount {
args: buffer(),
args_offset,
count_buffer: buffer(),
count_offset,
max_draw_count,
stride,
}
}
#[test]
fn a_mesh_indirect_draw_steps_three_words_per_structure() {
assert_eq!(
MESH_DISPATCH_ARGS_BYTES,
3 * size_of::<u32>() as u64,
"VkDrawMeshTasksIndirectCommandEXT, D3D12_DISPATCH_MESH_ARGUMENTS and \
MTLDispatchThreadgroupsIndirectArguments are the same three words"
);
assert_eq!(
plan_mesh_indirect(&indirect(0, 0, 12), 0).expect("a draw of nothing"),
None
);
let one = plan_mesh_indirect(&indirect(4, 1, 0), 16)
.expect("one structure at offset 4 of a 16-byte buffer")
.expect("a draw of one");
assert_eq!(one.first, 4);
assert_eq!(one.stride, MESH_DISPATCH_ARGS_BYTES);
assert_eq!(one.count, 1);
assert_eq!(one.offset(0), 4);
let tight = plan_mesh_indirect(&indirect(0, 4, 12), 48)
.expect("four tight structures fill a 48-byte buffer exactly")
.expect("a draw of four");
let offsets: Vec<u64> = (0..tight.count).map(|index| tight.offset(index)).collect();
assert_eq!(offsets, vec![0, 12, 24, 36]);
let padded = plan_mesh_indirect(&indirect(8, 3, 16), 8 + 16 * 2 + 12)
.expect("three structures 16 bytes apart from offset 8")
.expect("a draw of three");
let offsets: Vec<u64> = (0..padded.count)
.map(|index| padded.offset(index))
.collect();
assert_eq!(offsets, vec![8, 24, 40]);
for (draw, length, what) in [
(
indirect(2, 1, 0),
64,
"an offset that is not a multiple of four",
),
(indirect(0, 2, 8), 64, "a stride below one structure"),
(
indirect(0, 2, 14),
64,
"a stride that is not a multiple of four",
),
(indirect(0, 4, 12), 47, "four tight structures in 47 bytes"),
(
indirect(40, 1, 0),
48,
"one structure that starts 8 bytes from the end",
),
] {
let Err(error) = plan_mesh_indirect(&draw, length) else {
panic!("{what} must be refused");
};
assert!(
matches!(error, HalError::InvalidDescriptor(_)),
"{what}: {error:?}"
);
}
}
#[test]
fn check_layout_judges_the_offset_and_the_stride_and_not_the_bound() {
check_layout(&indirect(2, 0, 3), DRAW_ARGS_BYTES).expect("a draw of nothing");
check_layout(&indirect(4, 1, 0), DRAW_ARGS_BYTES)
.expect("one structure at offset 4 strides nowhere");
let past_the_end = indirect(1 << 40, 4, 32);
check_layout(&past_the_end, DRAW_ARGS_BYTES)
.expect("a bound is not this function's to judge");
assert!(
matches!(
plan_structures(&past_the_end, DRAW_ARGS_BYTES, 4096),
Err(HalError::InvalidDescriptor(_))
),
"the same draw must be refused where a length is available"
);
for (draw, args, what) in [
(
indirect(2, 1, 0),
DRAW_ARGS_BYTES,
"an offset that is not a multiple of four",
),
(
indirect(0, 2, 8),
DRAW_ARGS_BYTES,
"a stride below one 16-byte structure",
),
(
indirect(0, 2, 16),
DRAW_INDEXED_ARGS_BYTES,
"a stride below one 20-byte structure",
),
(
indirect(0, 2, 18),
DRAW_ARGS_BYTES,
"a stride that is not a multiple of four",
),
] {
let Err(error) = check_layout(&draw, args) else {
panic!("{what} must be refused");
};
assert!(
matches!(error, HalError::InvalidDescriptor(_)),
"{what}: {error:?}"
);
}
}
#[test]
fn a_count_limited_draw_checks_the_count_word_and_the_argument_array() {
assert_eq!(
plan_count_structures(&counted(2, 2, 0, 3), DRAW_ARGS_BYTES, 0, 0)
.expect("a draw of nothing"),
None
);
let tight = plan_count_structures(&counted(32, 4, 2, 16), DRAW_ARGS_BYTES, 64, 8)
.expect("two tight structures and a count that both fit exactly")
.expect("a draw of up to two");
assert_eq!(tight.first, 32);
assert_eq!(tight.stride, DRAW_ARGS_BYTES);
assert_eq!(tight.count, 2);
let offsets: Vec<u64> = (0..tight.count).map(|index| tight.offset(index)).collect();
assert_eq!(offsets, vec![32, 48]);
let one = plan_count_structures(&counted(0, 0, 1, 0), DRAW_INDEXED_ARGS_BYTES, 20, 4)
.expect("one indexed structure filling its buffer")
.expect("a draw of up to one");
assert_eq!(one.stride, DRAW_INDEXED_ARGS_BYTES);
let padded = plan_count_structures(&counted(8, 12, 3, 32), DRAW_ARGS_BYTES, 88, 16)
.expect("three structures 32 bytes apart from offset 8")
.expect("a draw of up to three");
let offsets: Vec<u64> = (0..padded.count)
.map(|index| padded.offset(index))
.collect();
assert_eq!(offsets, vec![8, 40, 72]);
for (draw, args, args_length, count_length, what) in [
(
counted(0, 2, 1, 0),
DRAW_ARGS_BYTES,
64,
16,
"a count offset that is not a multiple of four",
),
(
counted(0, 4, 1, 0),
DRAW_ARGS_BYTES,
64,
7,
"a count word whose last byte is past the end",
),
(
counted(0, u64::MAX - 3, 1, 0),
DRAW_ARGS_BYTES,
64,
8,
"a count offset whose end overflows",
),
(
counted(2, 0, 1, 0),
DRAW_ARGS_BYTES,
64,
8,
"an argument offset that is not a multiple of four",
),
(
counted(0, 0, 2, 8),
DRAW_ARGS_BYTES,
64,
8,
"a stride below one 16-byte structure",
),
(
counted(0, 0, 2, 18),
DRAW_ARGS_BYTES,
64,
8,
"a stride that is not a multiple of four",
),
(
counted(32, 4, 2, 16),
DRAW_ARGS_BYTES,
63,
8,
"two tight structures one byte past the argument buffer",
),
] {
let Err(error) = plan_count_structures(&draw, args, args_length, count_length) else {
panic!("{what} must be refused");
};
assert!(
matches!(error, HalError::InvalidDescriptor(_)),
"{what}: {error:?}"
);
}
}
}