Skip to content

Commit

Permalink
Gate usage of metal::ComputePassDescriptor on timestamp query suppo…
Browse files Browse the repository at this point in the history
…rt (#4643)
  • Loading branch information
teoxoy authored Nov 6, 2023
1 parent 261cb7c commit 1d4fa81
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions wgpu-hal/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,48 +1104,55 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
let raw = self.raw_cmd_buf.as_ref().unwrap();

objc::rc::autoreleasepool(|| {
let descriptor = metal::ComputePassDescriptor::new();

let mut sba_index = 0;
let mut next_sba_descriptor = || {
let sba_descriptor = descriptor
.sample_buffer_attachments()
.object_at(sba_index)
.unwrap();
sba_index += 1;
sba_descriptor
};
// TimeStamp Queries and ComputePassDescriptor were both introduced in Metal 2.3 (macOS 11, iOS 14)
// and we currently only need ComputePassDescriptor for timestamp queries
let encoder = if self.shared.private_caps.timestamp_query_support.is_empty() {
raw.new_compute_command_encoder()
} else {
let descriptor = metal::ComputePassDescriptor::new();

let mut sba_index = 0;
let mut next_sba_descriptor = || {
let sba_descriptor = descriptor
.sample_buffer_attachments()
.object_at(sba_index)
.unwrap();
sba_index += 1;
sba_descriptor
};

for (set, index) in self.state.pending_timer_queries.drain(..) {
let sba_descriptor = next_sba_descriptor();
sba_descriptor.set_sample_buffer(set.counter_sample_buffer.as_ref().unwrap());
sba_descriptor.set_start_of_encoder_sample_index(index as _);
sba_descriptor.set_end_of_encoder_sample_index(metal::COUNTER_DONT_SAMPLE);
}

for (set, index) in self.state.pending_timer_queries.drain(..) {
let sba_descriptor = next_sba_descriptor();
sba_descriptor.set_sample_buffer(set.counter_sample_buffer.as_ref().unwrap());
sba_descriptor.set_start_of_encoder_sample_index(index as _);
sba_descriptor.set_end_of_encoder_sample_index(metal::COUNTER_DONT_SAMPLE);
}
if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {
let sba_descriptor = next_sba_descriptor();
sba_descriptor.set_sample_buffer(
timestamp_writes
.query_set
.counter_sample_buffer
.as_ref()
.unwrap(),
);

if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {
let sba_descriptor = next_sba_descriptor();
sba_descriptor.set_sample_buffer(
timestamp_writes
.query_set
.counter_sample_buffer
.as_ref()
.unwrap(),
);
sba_descriptor.set_start_of_encoder_sample_index(
timestamp_writes
.beginning_of_pass_write_index
.map_or(metal::COUNTER_DONT_SAMPLE, |i| i as _),
);
sba_descriptor.set_end_of_encoder_sample_index(
timestamp_writes
.end_of_pass_write_index
.map_or(metal::COUNTER_DONT_SAMPLE, |i| i as _),
);
}

sba_descriptor.set_start_of_encoder_sample_index(
timestamp_writes
.beginning_of_pass_write_index
.map_or(metal::COUNTER_DONT_SAMPLE, |i| i as _),
);
sba_descriptor.set_end_of_encoder_sample_index(
timestamp_writes
.end_of_pass_write_index
.map_or(metal::COUNTER_DONT_SAMPLE, |i| i as _),
);
}
raw.compute_command_encoder_with_descriptor(descriptor)
};

let encoder = raw.compute_command_encoder_with_descriptor(descriptor);
if let Some(label) = desc.label {
encoder.set_label(label);
}
Expand Down

0 comments on commit 1d4fa81

Please sign in to comment.