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
use crate::{
    renderer::{BufferId, RenderContext, TextureId},
    texture::Extent3d,
};
use parking_lot::Mutex;
use std::sync::Arc;

#[derive(Clone, Debug)]
pub enum Command {
    CopyBufferToBuffer {
        source_buffer: BufferId,
        source_offset: u64,
        destination_buffer: BufferId,
        destination_offset: u64,
        size: u64,
    },
    CopyBufferToTexture {
        source_buffer: BufferId,
        source_offset: u64,
        source_bytes_per_row: u32,
        destination_texture: TextureId,
        destination_origin: [u32; 3],
        destination_mip_level: u32,
        size: Extent3d,
    },
    // TODO: Frees probably don't need to be queued?
    FreeBuffer(BufferId),
}

#[derive(Debug, Default, Clone)]
pub struct CommandQueue {
    // TODO: this shouldn't really need a mutex. it just needs to be shared on whatever thread it's scheduled on
    queue: Arc<Mutex<Vec<Command>>>,
}

impl CommandQueue {
    fn push(&mut self, command: Command) {
        self.queue.lock().push(command);
    }

    pub fn copy_buffer_to_buffer(
        &mut self,
        source_buffer: BufferId,
        source_offset: u64,
        destination_buffer: BufferId,
        destination_offset: u64,
        size: u64,
    ) {
        self.push(Command::CopyBufferToBuffer {
            source_buffer,
            source_offset,
            destination_buffer,
            destination_offset,
            size,
        });
    }

    #[allow(clippy::too_many_arguments)]
    pub fn copy_buffer_to_texture(
        &mut self,
        source_buffer: BufferId,
        source_offset: u64,
        source_bytes_per_row: u32,
        destination_texture: TextureId,
        destination_origin: [u32; 3],
        destination_mip_level: u32,
        size: Extent3d,
    ) {
        self.push(Command::CopyBufferToTexture {
            source_buffer,
            source_offset,
            source_bytes_per_row,
            destination_texture,
            destination_origin,
            destination_mip_level,
            size,
        });
    }

    pub fn free_buffer(&mut self, buffer: BufferId) {
        self.push(Command::FreeBuffer(buffer));
    }

    pub fn clear(&mut self) {
        self.queue.lock().clear();
    }

    pub fn execute(&mut self, render_context: &mut dyn RenderContext) {
        for command in self.queue.lock().drain(..) {
            match command {
                Command::CopyBufferToBuffer {
                    source_buffer,
                    source_offset,
                    destination_buffer,
                    destination_offset,
                    size,
                } => render_context.copy_buffer_to_buffer(
                    source_buffer,
                    source_offset,
                    destination_buffer,
                    destination_offset,
                    size,
                ),
                Command::CopyBufferToTexture {
                    source_buffer,
                    source_offset,
                    source_bytes_per_row,
                    destination_texture,
                    destination_origin,
                    destination_mip_level,
                    size,
                } => render_context.copy_buffer_to_texture(
                    source_buffer,
                    source_offset,
                    source_bytes_per_row,
                    destination_texture,
                    destination_origin,
                    destination_mip_level,
                    size,
                ),
                Command::FreeBuffer(buffer) => render_context.resources().remove_buffer(buffer),
            }
        }
    }
}