1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! Command queue management for GPU operations
//!
//! This module provides abstractions for managing command queues,
//! including command buffer submission, synchronization, and queue families.
use crate::GpuDevice;
use std::sync::Arc;
use wgpu::{CommandBuffer, CommandEncoder, Queue, SubmissionIndex};
/// Queue type for different workload categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum QueueType {
/// Compute queue for compute operations
Compute,
/// Transfer queue for data transfers
Transfer,
/// Graphics queue for graphics operations (rarely used in compute-only context)
Graphics,
}
/// Command queue wrapper with additional functionality
pub struct CommandQueue {
queue: Arc<Queue>,
device: Arc<wgpu::Device>,
queue_type: QueueType,
}
impl CommandQueue {
/// Create a new command queue
#[must_use]
pub fn new(device: &GpuDevice, queue_type: QueueType) -> Self {
Self {
queue: Arc::clone(device.queue()),
device: Arc::clone(device.device()),
queue_type,
}
}
/// Submit a single command buffer to the queue
///
/// # Arguments
///
/// * `command_buffer` - Command buffer to submit
///
/// # Returns
///
/// Submission index for synchronization
#[must_use]
pub fn submit_single(&self, command_buffer: CommandBuffer) -> SubmissionIndex {
self.queue.submit(Some(command_buffer))
}
/// Submit multiple command buffers to the queue
///
/// # Arguments
///
/// * `command_buffers` - Command buffers to submit
///
/// # Returns
///
/// Submission index for synchronization
#[must_use]
pub fn submit_many(&self, command_buffers: Vec<CommandBuffer>) -> SubmissionIndex {
self.queue.submit(command_buffers)
}
/// Submit commands created by an encoder
///
/// # Arguments
///
/// * `encoder` - Command encoder to finish and submit
///
/// # Returns
///
/// Submission index for synchronization
#[must_use]
pub fn submit_encoder(&self, encoder: CommandEncoder) -> SubmissionIndex {
self.queue.submit(Some(encoder.finish()))
}
/// Write data directly to a buffer
///
/// This is a convenience method that bypasses the staging buffer
/// and directly writes to the destination buffer.
///
/// # Arguments
///
/// * `buffer` - Target buffer
/// * `offset` - Offset in bytes
/// * `data` - Data to write
pub fn write_buffer(&self, buffer: &wgpu::Buffer, offset: u64, data: &[u8]) {
self.queue.write_buffer(buffer, offset, data);
}
/// Wait for all pending operations on this queue to complete
pub fn wait(&self) {
let _ = self.device.poll(wgpu::PollType::wait_indefinitely());
}
/// Get the queue type
#[must_use]
pub fn queue_type(&self) -> QueueType {
self.queue_type
}
/// Get the underlying WGPU queue
#[must_use]
pub fn queue(&self) -> &Arc<Queue> {
&self.queue
}
}
/// Queue manager for multi-queue support
pub struct QueueManager {
compute_queue: CommandQueue,
transfer_queue: CommandQueue,
graphics_queue: CommandQueue,
}
impl QueueManager {
/// Create a new queue manager
///
/// Note: In wgpu, we typically have a single queue that handles all operations.
/// This abstraction provides a logical separation for different workload types.
#[must_use]
pub fn new(device: &GpuDevice) -> Self {
Self {
compute_queue: CommandQueue::new(device, QueueType::Compute),
transfer_queue: CommandQueue::new(device, QueueType::Transfer),
graphics_queue: CommandQueue::new(device, QueueType::Graphics),
}
}
/// Get the compute queue
#[must_use]
pub fn compute(&self) -> &CommandQueue {
&self.compute_queue
}
/// Get the transfer queue
#[must_use]
pub fn transfer(&self) -> &CommandQueue {
&self.transfer_queue
}
/// Get the graphics queue
#[must_use]
pub fn graphics(&self) -> &CommandQueue {
&self.graphics_queue
}
/// Get a queue by type
#[must_use]
pub fn get_queue(&self, queue_type: QueueType) -> &CommandQueue {
match queue_type {
QueueType::Compute => &self.compute_queue,
QueueType::Transfer => &self.transfer_queue,
QueueType::Graphics => &self.graphics_queue,
}
}
/// Wait for all queues to complete
pub fn wait_all(&self) {
self.compute_queue.wait();
self.transfer_queue.wait();
self.graphics_queue.wait();
}
}
/// Command buffer builder with fluent API
pub struct CommandBufferBuilder {
encoder: CommandEncoder,
label: String,
}
impl CommandBufferBuilder {
/// Create a new command buffer builder
pub fn new(device: &GpuDevice, label: impl Into<String>) -> Self {
let label_string = label.into();
let encoder = device
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some(&label_string),
});
Self {
encoder,
label: label_string,
}
}
/// Get a mutable reference to the encoder
pub fn encoder(&mut self) -> &mut CommandEncoder {
&mut self.encoder
}
/// Finish building and return the command buffer
#[must_use]
pub fn finish(self) -> CommandBuffer {
self.encoder.finish()
}
/// Finish building and submit to a queue
#[must_use]
pub fn submit(self, queue: &CommandQueue) -> SubmissionIndex {
queue.submit_encoder(self.encoder)
}
/// Get the label
#[must_use]
pub fn label(&self) -> &str {
&self.label
}
}
/// Async command submission handle
pub struct AsyncSubmission {
submission_index: SubmissionIndex,
device: Arc<wgpu::Device>,
}
impl AsyncSubmission {
/// Create a new async submission handle
#[must_use]
pub fn new(submission_index: SubmissionIndex, device: Arc<wgpu::Device>) -> Self {
Self {
submission_index,
device,
}
}
/// Wait for this submission to complete
pub fn wait(&self) {
let _ = self.device.poll(wgpu::PollType::wait_indefinitely());
}
/// Get the submission index
#[must_use]
pub fn index(&self) -> &SubmissionIndex {
&self.submission_index
}
}
/// Batch command submission for improved performance
pub struct BatchSubmitter {
command_buffers: Vec<CommandBuffer>,
max_batch_size: usize,
}
impl BatchSubmitter {
/// Create a new batch submitter
///
/// # Arguments
///
/// * `max_batch_size` - Maximum number of command buffers to batch
#[must_use]
pub fn new(max_batch_size: usize) -> Self {
Self {
command_buffers: Vec::with_capacity(max_batch_size),
max_batch_size,
}
}
/// Add a command buffer to the batch
///
/// If the batch is full, it will be automatically submitted.
///
/// # Arguments
///
/// * `command_buffer` - Command buffer to add
/// * `queue` - Queue to submit to when batch is full
///
/// # Returns
///
/// Submission index if batch was submitted, None otherwise
pub fn add(
&mut self,
command_buffer: CommandBuffer,
queue: &CommandQueue,
) -> Option<SubmissionIndex> {
self.command_buffers.push(command_buffer);
if self.command_buffers.len() >= self.max_batch_size {
Some(self.flush(queue))
} else {
None
}
}
/// Flush all pending command buffers to the queue
///
/// # Arguments
///
/// * `queue` - Queue to submit to
///
/// # Returns
///
/// Submission index
pub fn flush(&mut self, queue: &CommandQueue) -> SubmissionIndex {
let buffers = std::mem::take(&mut self.command_buffers);
queue.submit_many(buffers)
}
/// Get the number of pending command buffers
#[must_use]
pub fn pending_count(&self) -> usize {
self.command_buffers.len()
}
/// Check if the batch is empty
#[must_use]
pub fn is_empty(&self) -> bool {
self.command_buffers.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_queue_type() {
assert_eq!(QueueType::Compute, QueueType::Compute);
assert_ne!(QueueType::Compute, QueueType::Transfer);
assert_ne!(QueueType::Compute, QueueType::Graphics);
}
#[test]
fn test_batch_submitter() {
let submitter = BatchSubmitter::new(5);
assert_eq!(submitter.pending_count(), 0);
assert!(submitter.is_empty());
}
}