Skip to content

Commit

Permalink
Add support for creating and using indirect command buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
konsolas authored and kvark committed Jul 23, 2023
1 parent efc673f commit f8926a9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,19 @@ impl DeviceRef {
}
}

pub fn new_indirect_command_buffer_with_descriptor(
&self,
descriptor: &IndirectCommandBufferDescriptorRef,
max_command_count: NSUInteger,
options: MTLResourceOptions,
) -> IndirectCommandBuffer {
unsafe {
msg_send![self, newIndirectCommandBufferWithDescriptor:descriptor
maxCommandCount:max_command_count
options:options]
}
}

pub fn new_texture(&self, descriptor: &TextureDescriptorRef) -> Texture {
unsafe { msg_send![self, newTextureWithDescriptor: descriptor] }
}
Expand Down
68 changes: 68 additions & 0 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,45 @@ impl BlitCommandEncoderRef {
unsafe { msg_send![self, waitForFence: fence] }
}

pub fn copy_indirect_command_buffer(
&self,
source: &IndirectCommandBufferRef,
source_range: NSRange,
destination: &IndirectCommandBufferRef,
destination_index: NSUInteger,
) {
unsafe {
msg_send![self,
copyIndirectCommandBuffer: source
sourceRange: source_range
destination: destination
destinationIndex: destination_index
]
}
}

pub fn reset_commands_in_buffer(&self, buffer: &IndirectCommandBufferRef, range: NSRange) {
unsafe {
msg_send![self,
resetCommandsInBuffer: buffer
withRange: range
]
}
}

pub fn optimize_indirect_command_buffer(
&self,
buffer: &IndirectCommandBufferRef,
range: NSRange,
) {
unsafe {
msg_send![self,
optimizeIndirectCommandBuffer: buffer
withRange: range
]
}
}

/// See: <https://developer.apple.com/documentation/metal/mtlblitcommandencoder/3194348-samplecountersinbuffer>
pub fn sample_counters_in_buffer(
&self,
Expand Down Expand Up @@ -1861,6 +1900,35 @@ impl ArgumentEncoderRef {
unsafe { msg_send![self, constantDataAtIndex: at_index] }
}

pub fn set_indirect_command_buffer(
&self,
at_index: NSUInteger,
buffer: &IndirectCommandBufferRef,
) {
unsafe {
msg_send![self,
setIndirectCommandBuffer: buffer
atIndex: at_index
]
}
}

pub fn set_indirect_command_buffers(
&self,
start_index: NSUInteger,
data: &[&IndirectCommandBufferRef],
) {
unsafe {
msg_send![self,
setIndirectCommandBuffers: data.as_ptr()
withRange: NSRange {
location: start_index,
length: data.len() as _,
}
]
}
}

pub fn new_argument_encoder_for_buffer(&self, index: NSUInteger) -> ArgumentEncoder {
unsafe {
let ptr = msg_send![self, newArgumentEncoderForBufferAtIndex: index];
Expand Down
7 changes: 7 additions & 0 deletions src/indirect_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ foreign_obj_type! {
pub struct IndirectCommandBufferDescriptor;
}

impl IndirectCommandBufferDescriptor {
pub fn new() -> Self {
let class = class!(MTLIndirectCommandBufferDescriptor);
unsafe { msg_send![class, new] }
}
}

impl IndirectCommandBufferDescriptorRef {
pub fn command_types(&self) -> MTLIndirectCommandType {
unsafe { msg_send![self, commandTypes] }
Expand Down

0 comments on commit f8926a9

Please sign in to comment.